Select posts using @wordpress/data with query based on custom field always returns all records

It appears that REST API doesn’t support all the meta_query parameters that WP_Query does. Only the basic meta queries with some comparison operators are supported. Therefore we need to make one of workarounds to get what we want: Add some REST API Filter to our theme/plugin add_filter(‘rest_{post_type}_query’, function($args, $request) { $params = $request->get_params(); if(isset($params[‘meta_compare’])) { … Read more

How to order product for key exists?

The meta_key parameter tells WP_Query to only return posts that have the price_sale_custom meta data. Remove that first, and then change orderby to reference the meta_query values: This is something that WordPress added support for in 4.2: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/ https://stackoverflow.com/questions/17745334/how-to-order-by-multiple-meta-keys

How to compare Event period with Week period using get_posts(), meta_query, relation and compare

To be able to do this your post meta values need to be strings in a valid date format, e.g. 2022/01/31 for January 31st 2022. Note I used ISO standard timestamp format with the biggest value first, then everything descending, and avoided using a regional value such as 31/1/2022 from the UK, or 1/31/2023 from … Read more

Order posts base on last favorite post by user

To order favorite posts based on the last post favorited by a user, you need to modify the query to include the post IDs in the order they were favorited by the user WP does not directly store the order of favorites, you might need to store this information separately, for example, in user meta … Read more

Meta query array – same order as specified order in value

Use PHP to sort the results after the query (untested): $product_ids = array( 3624, 1298, 430, 5629, 765 ); $query = new WP_Query( array( ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘product_id’, ‘value’ => $product_ids, ‘compare’ => ‘IN’, ), ), ) ); $posts_by_product_id = array(); $sorted_posts = array(); if ( $query->have_posts() ) { … Read more