Need to be able to sort the results of a REST API custom post query by a meta value.
Having difficulty doing so.
I have made my post type available to the REST API and can order by the Date, Title, etc...
But when I try the Post Meta it doesn't work.
I have added the following code to try and enable the functionality but defaults to ordering by date.
function my_add_meta_vars ($current_vars) {
$current_vars = array_merge ($current_vars, array('meta_key', 'meta_value'));
return $current_vars;
}
add_filter ('query_vars', 'my_add_meta_vars');
add_filter ('rest_query_vars', 'my_add_meta_vars');
My REST API query is
mysite.com/wp-json/wp/v2/hh_equipment?filter[orderby]=meta_value_num&meta_key=equipment_price&order=desc
I have tried following the instructions here to no avail.
Running WordPress 4.8 and tried testing on 4.7 to no avail
I've got it working with the rest_' . $post_type . '_collection_params filter and rest_' . $post_type . '_query filter like so (change $post_type to needed post type slug):
// Add meta your meta field to the allowed values of the REST API orderby parameter
add_filter(
'rest_' . $post_type . '_collection_params',
function( $params ) {
$params['orderby']['enum'][] = 'YOUR_META_KEY';
return $params;
},
10,
1
);
// Manipulate query
add_filter(
'rest_' . $post_type . '_query',
function ( $args, $request ) {
$order_by = $request->get_param( 'orderby' );
if ( isset( $order_by ) && 'YOUR_META_KEY' === $order_by ) {
$args['meta_key'] = $order_by;
$args['orderby'] = 'meta_value'; // user 'meta_value_num' for numerical fields
}
return $args;
},
10,
2
);
The first filter adds your meta field to the possible values of the ordeby parameters, as by default REST API supports only: author, date, id, include, modified, parent, relevance, slug, include_slugs, title (check the ordeby param in the WP REST API handbook)
The second filter allows you to manipulate the query that returns the results when you have your meta key inside the orderby. Here we need to reset orderby to 'meta_value' or 'meta_value_num' (read more about this in WP Query class description) and set the meta key to your custom field key.
Refer below method,
I modified the existing routes to add a new args entry which validates the meta_key values which are permitted. No need to modify the rest query vars this way either.
add_filter('rest_endpoints', function ($routes) {
// I'm modifying multiple types here, you won't need the loop if you're just doing posts
foreach (['some', 'types'] as $type) {
if (!($route =& $routes['/wp/v2/' . $type])) {
continue;
}
// Allow ordering by my meta value
$route[0]['args']['orderby']['enum'][] = 'meta_value_num';
// Allow only the meta keys that I want
$route[0]['args']['meta_key'] = array(
'description' => 'The meta key to query.',
'type' => 'string',
'enum' => ['my_meta_key', 'another key'],
'validate_callback' => 'rest_validate_request_arg',
);
}
return $routes;
});
REF: https://github.com/WP-API/WP-API/issues/2308
Related
Been a little stumped after researching and was curious if anyone could provide assistance.
I was curious if there is a way to tap into the WC API and retrieve and order using the endpoint /wp-json/wc/v3/orders but based off of the following meta data:
"id": 3944240,
"key": "_alg_wc_full_custom_order_number",
"value": "D########"
I would like to get an order off that meta data key value of D###### rather than order id or any other parameters their documentation provides, if not possible totally understand just figured id try here!
Thanks!
AFAIK, the WC shop order endpoint doesn't accept meta_key parameter out of the box. But you can hook to the endpoint filter with custom function as below, then you can use the endpoint as such:
/wp-json/wc/v3/orders?custom_order_number=D#####
add_filter( 'woocommerce_rest_shop_order_object_query', 'filter_orders_by_custom_order_number', 100, 2 );
function filter_orders_by_custom_order_number( $args, $request ) {
if( !isset( $request['custom_order_number'] ) ) {
return $args;
}
// add custom query to existing meta_query if exist
if( isset( $args['meta_query'] ) ) {
$args['meta_query']['relation'] = 'AND';
} else {
$args['meta_query'] = array();
}
$args['meta_query'][] = array(
'key' => '_alg_wc_full_custom_order_number',
'value' => $request['custom_order_number']
);
return $args;
}
I'm looking for a way to extend the search field in WooCommerce admin orders list for a custom meta key. Currently i'm using the woocommerce_shop_order_search_fields filter hook.
Resulting in this code, which allows me to search by the user id, order total and order number.
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields ) {
$search_fields[] = '_order_total';
$search_fields[] = '_user_id';
$search_fields[] = '_order_number';
return $search_fields;
}
However, these are all existing meta keys, what if I want to search for meta data that doesn't yet exist? Any adivce?
Expanding the search in WooCommerce admin orders list can be done very easily by using the woocommerce_shop_order_search_fields filter hook, you just need to add some post_meta fields for order (item(s)). As long as this data exists of course!
By default, the following metakeys are present:
_billing_address_index
_shipping_address_index
_billing_last_name
_billing_email
So, for example, if you want to extend the search by the billing first name, you can do this by simply adding the metakey _billing_first_name, and then you get:
function filter_woocommerce_shop_order_search_fields( $search_fields ) {
// Metakey
$search_fields[] = '_billing_first_name';
return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'filter_woocommerce_shop_order_search_fields', 10, 1 );
Now, what if this metadata is not present for the orders? You could add that specific metadata for future orders, when the order is created. But wait! what about the existing orders? for these orders this data would not exist!
Wouldn't it be useful if we could add this data to existing orders, or even newer orders. Well this is possible, namely by using wc_get_orders(), to get (and update) the existing orders before running the search.
So you get: (where we in this example add the user's nickname as meta data to orders)
function filter_woocommerce_shop_order_search_fields( $search_fields ) {
// The desired meta key
$meta_key = '_user_nickname';
// Get ALL orders where a certain meta key not exists
$orders = wc_get_orders( array(
'limit' => -1, // Query all orders
'meta_key' => $meta_key, // Post meta_key
'meta_compare' => 'NOT EXISTS', // Comparison argument
));
// NOT empty
if ( ! empty ( $orders ) ) {
// Loop through the orders
foreach ( $orders as $order ) {
// Get the desired information via the order object
// Get user
$user = $order->get_user();
// User is NOT empty
if ( ! empty ( $user ) ) {
// Get nickname from user
$meta_value = $user->nickname;
// Meta value is NOT empty
if ( ! empty ( $meta_value ) ) {
// Add the meta data
$order->update_meta_data( $meta_key, $meta_value );
$order->save();
}
}
}
}
// Metakey
$search_fields[] = $meta_key;
return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'filter_woocommerce_shop_order_search_fields', 10, 1 );
Trying to create new fields for my form dynamically, as I'm getting json from 3rd party API. Based on this json, I need a number of fields added to my form - not fixed number. So, I'm doing this, hooking it onto gform_pre_render:
add_filter( 'gform_pre_process', 'create_products_gforms' );
add_filter( 'gform_admin_pre_render', 'create_products_gforms' );
add_filter( 'gform_pre_render', 'create_products_gforms' );
function create_products_gforms( $form ) {
$helper = new NSHelper();
$state_name = $_POST['input_7'] ?? '';
$code_value = $helper->get_state_code_by_name( $state_name );
// Only fetch products if current form id is 33, state code is defined
// and if there are products for this state.
if ( $form['id'] != 33 || !$code_value ) {
return $form;
}
// Get product list from NetSuit API based on state code
$products_json_data = get_products_data( $code_value );
$products = json_decode( json_decode( $products_json_data ) );
$new_field_id = 0;
foreach( $form['fields'] as $field ) {
if( $field->id > $new_field_id ) {
$new_field_id = $field->id;
}
}
$new_field_id++;
foreach ( $products as $product_object ) {
// Prepare field properties
$props = array(
'id' => $new_field_id,
'type' => 'singleproduct',
'label' => $product_object->onlinedisplayname,
'basePrice' => floatval( $product_object->price ),
'enableCalculation' => true
);
// Create new gravity forms field and add it to the form object
$nf = GF_Fields::create( $props );
// Hack - insert into array at specific position
// Needed to display product fields before other fields
// in the form
array_splice( $form['fields'], 11, 0, array($nf) );
$new_field_id++;
}
GFAPI::update_form( $form );
$form['dynamic_fields_ids'] = $added_fields_ids;
return $form;
}
This works, ie, it shows fields properly on the frontend. Now, the issue with this is that, once form is submitted, all the fields except these dynamically added ones are in the submission. But these are not. I assumed this has to do that these fields aren't registered in the form, so I also tried GFAPI::update_form( $form );, but that didn't help with submission part, though it udpates my form with new fields in the backend too.
Any ideas?
Based on your use-case, Milos, I would suggest using the gform_form_post_get_meta filter:
https://docs.gravityforms.com/gform_form_post_get_meta/
This will fire every time the form is fetched from the database and the most sure-fire way of guaranteeing your fields will be present.
If you prefer to be more surgical and stick with the gform_pre_render approach, you'll want to apply the same function on a couple other filters:
gform_pre_process
gform_admin_pre_render
I have a custom post type that supports password protected entries. In a custom loop using a new WP_Query object, I want to exclude those password protected posts from the results. What arguments do I need set in order to do this? I am using the latest trunk version of WordPress 3.2.1.
I come up to this question where I was looking for the same. However, I read WP_Query document line by line then found very simple solution and that is just to add 'has_password' => false argument to the query $args
So the code will be as below...
$args = [
'post_type' => [ 'post', 'page' ],
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' ),
'has_password' => FALSE
];
Here you can see I have excluded Sticky and Password Protected posts.
I really like Kevin's approach, but I adjusted it slightly:
// Create a new filtering function that will add our where clause to the query
function my_password_post_filter( $where = '' ) {
// Make sure this only applies to loops / feeds on the frontend
if (!is_single() && !is_admin()) {
// exclude password protected
$where .= " AND post_password = ''";
}
return $where;
}
add_filter( 'posts_where', 'my_password_post_filter' );
Did you take a look at the post_status argument of WP_Query?
"Protected" seems like a good candidate to exclude.
Edit: Okay, it seems like you'll have to modify the where clause to achieve what you want:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// exclude password protected
$where .= " AND post_password = ''";
return $where;
}
if (!is_single()) { add_filter( 'posts_where', 'filter_where' ); }
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
After a bit of playing about, I found the posts_where filter a bit too intrusive for what I wanted to do, so I came up with an alternative. As part of the 'save_post' action that I attached for my custom post type, I added the following logic;
$visibility = isset($_POST['visibility']) ? $_POST['visibility'] : '';
$protected = get_option('__protected_posts', array());
if ($visibility === 'password' && !in_array($post->ID, $protected)) {
array_push($protected, $post->ID);
}
if ($visibility === 'public' && in_array($post->ID, $protected)) {
$i = array_search($post->ID, $protected);
unset($protected[$i]);
}
update_option('__protected_posts', $protected);
What this does is hold an array of post id's in the options table where the post is protected by a password. Then in a custom query I simply passed this array as part of the post__not_in option e.g.
$query = new WP_Query(array(
'post_type' => 'my_custom_post_type',
'post__not_in' => get_option('__protected_posts'),
));
This way I could exclude the protected posts from an archive page but still allow a user to land on the password protected page to enter the password.
In addition to #Peter Chester's answer:
You may also want to exclude password-protected posts from the Previous Post and Next Post links, if you have those at the bottom of your post page.
To do so you can add the exclusion to the get_previous_post_where and get_next_post_where hooks.
add_filter( 'get_previous_post_where', 'my_theme_mod_adjacent' );
add_filter( 'get_next_post_where', 'my_theme_mod_adjacent' );
function my_theme_mod_adjacent( $where ) {
return $where . " AND p.post_password = ''";
}
I'm trying to update the post content in one of my post through the wp_update_post function. I have read the documentation here: http://codex.wordpress.org/Function_Reference/wp_update_post
And if I get it right I just need to send the post ID and the post content I want to update with - just like in the example - and this should be the only thing that will change. Although my custom fields that I have attached to this post disappears, strange enough.
I have the following code that I pass on:
if(isset($_POST['submit'])){
$the_post = array();
$the_post['ID'] = $_POST['id'];
$the_post['post_content'] = $_POST['recension'];
// Update the post into the database
wp_update_post( $the_post );
}
How come this happen and how do I solve it?
This is because when you are updating the post the *wp_insert_post* function is used and there is "save_post"(1) action hook which is usually used for saving custom fields data.
The standard way to add/update post meta is something like this:
$post_meta['_mymeta'] = $_POST['_mymeta'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $post_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
if($value && $value != get_post_meta($post->ID, $key, TRUE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} elseif($value && get_post_meta($post_id, $key, TRUE) == "") { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value, TRUE);
}
if(!$value) delete_post_meta($post->ID, $key, get_post_meta($post->ID, $key, TRUE)); // Delete if blank
}
...as you can see it is checking for *$_POST* data and if it is empty or not set it updates your meta value with empty data or deletes it completely.
I suppose you should use database update function or some other API function to update post fields...for example this piece of code will update your post menu order:
$wpdb->update( $wpdb->posts, array( 'menu_order' => 5 ), array( 'ID' => $post->ID ) );
(1) Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. Action function arguments: post ID.
To avoid that behavior set false as third parameter.
It deactivates the "after insert hooks".