How to get raw values in latest Wordpress Rest API v2? - wordpress

When requesting posts/pages/media with Wordpress Rest API v2 I used to receive a 'raw' and a 'rendered' value for fields like title, guid and content. With the latest Wordpress version the 'raw' fields seems to have vanished. I need the raw data as this is stable over time. Plugins can add (changing) data to the rendered data.
Is there a (header) parameter I need to provide in the API call to get raw data?
Original data returned (fragment):
Array
(
[id] => 1016
[date] => 2017-11-08T16:18:29
[date_gmt] => 2017-11-08T15:18:29
[guid] => Array
(
[rendered] => https://example.com/wp-content/uploads/2017/03/image.jpg
[raw] => https://example.com/wp-content/uploads/2017/03/image.jpg
)
[modified] => 2017-11-08T16:18:39
[modified_gmt] => 2017-11-08T15:18:39
[slug] => 888
[status] => inherit
[type] => attachment
[link] => https://example.com/review/shopper/attachment/test/
[title] => Array
(
[raw] => shopper image
[rendered] => shopper image
)
Latest WP API data:
Array
(
[id] => 1016
[date] => 2017-11-08T16:18:29
[date_gmt] => 2017-11-08T15:18:29
[guid] => Array
(
[rendered] => https://example.com/wp-content/uploads/2017/03/image.jpg
)
[modified] => 2017-11-08T16:18:39
[modified_gmt] => 2017-11-08T15:18:39
[slug] => 888
[status] => inherit
[type] => attachment
[link] => https://example.com/review/shopper/attachment/test/
[title] => Array
(
[rendered] => shopper image
)
The API URL:
https://example.com/wp-json/wp/v2/media/32
Edit:
I'm using oAuth1 for authentication (https://wordpress.org/plugins/rest-api-oauth1/). Given that raw values are not returned unauthenticated, I'm starting to suspect that this plugin has issues sending the authentication during the information gathering.

You have to pass 'context' = 'edit' to get the raw content.
Or you can use this plugin: https://github.com/w1z2g3/wordpress-plugins/blob/master/post-raw-content.php

Related

WordPress WP_Query Limited to 5 Meta Query Clauses

The query works if you change "publish" to "draft" or remove the source clause. These are args passed to the WP_Query constructor. I immediately count the results to verify that WP_Query is in fact returning no results. I checked the data in the database and made sure the data is congruent.
Array
(
[post_type] => post
[post_status] => Array
(
[0] => publish
)
[meta_query] => Array
(
[view_count_clause] => Array
(
[key] => _viewcount
[type] => NUMERIC
)
[actual_date_clause] => Array
(
[key] => _actualdate
)
[start_date_clause] => Array
(
[key] => _actualdate
[value] => 2021-03-23 00-00-00
[compare] => >
)
[end_date_clause] => Array
(
[key] => _actualdate
[value] => 2021-03-24 00-00-00
[compare] => <
)
[color_clause] => Array
(
[key] => _color
[type] => NUMERIC
[compare] => IN
[value] => Array
(
[0] => 4
)
)
[source_clause] => Array
(
[key] => _sourcename
[value] => AHA
[compare] => =
)
)
[orderby] => Array
(
[actual_date_clause] => DESC
)
)
Related question here.... How to avoid the limit on the number of AND conditions for custom field (meta value) queries in WordPress get_posts()?
Problem:
WP_Query limits meta query clauses to five meta clauses. If you have 6 meta query clauses, you will get no results.
Solution 1:
Write your own SQL query. Example provided by the related question.
Solution 2:
Remove excess meta queries beyond the 5 meta query limit for WP_Query and manually filter results. Choice of meta queries to be removed should be based on whatever would result in the smallest data set.

Meta Query being igored in WP

I am attempting a meta query to get between two prices set as a meta field in my custom post.
However the _ccprop_prop_price query is being ignored. Its still giving results higher than 100000 , I can confirm the id of the meta field is _ccprop_prop_price. It is worth noting however that the field is a text field not a number field.
Am I missing something obvious? I even tried to break it down further by doing two arrays and using < and > instead.
Edit: Just another development, I notice = does in fact work which makes me wonder is it something to do with it being a text field.
Here is the resulting final $args
(
[post_type] => property
[orderby] => meta_value_num
[meta_key] => _ccprop_prop_price
[order] => ASC
[posts_per_page] => 30
[paged] => 1
[tax_query] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[key] => _ccprop_prop_price
[value] => Array
(
[0] => 0
[1] => 100000
)
[compare] => BETWEEN
[type] => NUMERIC
)
)
)
I found the issue, there were commas for some of the prices in that meta field and they were the ones appearing.

Wordpress taxonomy not displaying

I have a custom taxonomy called manufacturer. It's created via an init hook. I've verified that the taxonomy exists in wordpress by print_r(get_taxonomies()); I've also verified that a post has the taxonomy by doing print_r(get_the_terms(66878, 'manufacturer')); which shows me this:
Array ( [0] => WP_Term Object ( [term_id] => 6957 [name] => Agilent [slug] => 6-agilent [term_group] => 0 [term_taxonomy_id] => 6957 [taxonomy] => manufacturer [description] => [parent] => 0 [count] => 889 [filter] => raw ) )
The site was working fine up until a few days ago. I verified the DB. Innodb and nothing appears corrupted. The data in the DB looks correct. If I run this code I get no results:
$args = array(
'taxonomy' => 'manufacturer',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0
);
$all_categories = get_categories( $args );
or this:
print_r(get_terms( 'manufacturer', [
'hide_empty' => false,
]));
Git shows no file changes. Also, dev and live both have this issue even though dev has not been updated data-wise in weeks. The only event that happened recently was the server got restored from an image. After the restoration, the manufacturer taxonomy was working properly. Also, this is not the only custom taxonomy that isn't functional anymore. There is at least one other one.
I went to edit the wp-config.php file on the server and I couldn't save the file. The disk was full.
While the docs says get_categories can be used here, I always prefer to use get_terms when retrieving taxonomy terms. I don't think this will make the difference but that would be my first attempt.
Furthermore, you defined some default values, which are not needed imo:
hierarchical is true by default
orderby is name by default
pad_counts is false by default
I wouldn't define what is default.

drupal conditional action with custom php code

When adding custom php code for a conditional action, drupal/ubercart provides two php variables ($order and $account) that hold information about the order and account for the checkout that was just completed.
I can't find anywhere on the internet documentation about how these objects are structured...anyone know where this documentation is or how these objects are setup???
thanks
Here's what the $order object looks like on a fairly standard install (might be a bit different depending on your installation. Of course the products in the order dictate what the items section looks like:
stdClass Object
(
[order_id] => 123
[uid] => 456
[order_status] => payment_received
[order_total] => 100
[product_count] => 1
[primary_email] => test#example.com
[delivery_first_name] => Test
[delivery_last_name] => Customer
[delivery_phone] => 123-123-1234
[delivery_company] => ABC Company, Inc.
[delivery_street1] => 123 Easy St.
[delivery_street2] =>
[delivery_city] => Anytown
[delivery_zone] => 39
[delivery_postal_code] => 12345
[delivery_country] => 840
[billing_first_name] => Test
[billing_last_name] => Customer
[billing_phone] => 123-123-1234
[billing_company] => ABC Company, Inc.
[billing_street1] => 123 Easy St.
[billing_street2] =>
[billing_city] => Anytown
[billing_zone] => 39
[billing_postal_code] => 12345
[billing_country] => 840
[payment_method] => credit
[data] => Array
(
[cc_data] => ***encrypted credit card data***
)
[created] => 1295455508
[modified] => 1295457962
[host] => 127.0.0.1
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 245
[order_id] => 123
[nid] => 5
[title] => Test Product
[manufacturer] =>
[model] => TEST-PRODUCT-SKU
[qty] => 1
[cost] => 100.00000
[price] => 100.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[module] => uc_product
)
[order_uid] => 456
)
)
[payment_details] =>
[quote] => Array
(
[method] => flatrate_1
[accessorials] => 0
[rate] => 7.00000
[quote_form] =>
)
[line_items] => Array
(
[0] => Array
(
[line_item_id] => subtotal
[type] => subtotal
[title] => Subtotal
[amount] => 100
[weight] => 0
[data] =>
)
[1] => Array
(
[line_item_id] => 194
[type] => shipping
[title] => Flat Rate Shipping
[amount] => 7.00000
[weight] => 1
[data] =>
)
)
)
$account is the user object.
$order is the ubercart order object.
There are some minimum defined values for both of these objects, but they can contain anything really. The reason is, that Drupal will allow modules to expand the user object, while ubercart will allow modules to expand the order object.
The best thing to do in such situations is to inspect the objects to figure out how to get to what you need.
The devel module will allow you to pretty print variables using dsm() or dump the variable t a log file using dd(). Those are two ways to get to the variable info.

Can I have fixed cropping with images?

How can I specify a default value and hide this checkbox ?
[states] => Array
(
[#type] => checkboxes
[#title] => Status
[#options] => Array
(
[active] => Active users
[inactive] => Inactive users
)
[#description] => Subscriptions matching the selected states will be exported.
[#required] => 1
)
I've tried
$form['states']['#default_value'] = 'active';
but it gives me an error..
thanks
Your code as written will produce a syntax error, since you're declaring the 'states' array wrong. This is a PHP syntax thing that doesn't really have anything to do with Drupal's forms API.
Try:
$form['states'] => Array (
'#type' => 'checkboxes',
'#title' => t('Status'),
'#options' => Array (
'active' => 'Active users',
'inactive' => 'Inactive users',
),
'#description' => t('Subscriptions matching the selected states will be exported.'),
'#required' => 1,
'#default_value' => 'active',
);
Unrelated to the syntax issue, a better way to do this would be to reset the entire form element as a '#type' = 'value'. For example $form['states']['#type'] = 'value'; $form['states']['value'] = 'whatever'

Resources