The query filter
-
Hi!
I really wanted to show in Similar posts only those products of the online store that are available. The availability of the item is defined in the custom field ‘sklad_1’.
I know how to modify a query to get products with the required custom fields. But I didn’t want to change the plugin files. And to change (filter) the query, you need a filter on it. I temporarily changed the plugin file YARPP_Core.php and added a filter to the query in the function display_related():
$wp_query->query( apply_filters('yarpp_wp_query', array( // malferov - apply_filters (see functions.php of theme) 'p' => $reference_ID, 'orderby' => $orders[0], 'order' => $orders[1], 'showposts' => $limit, 'post_type' => (isset($args['post_type']) ? $args['post_type'] : $this->get_post_types()), ), $args) );
And in the functions file.php themes added filter:
/* * Filter wp_query of YARPP Plugin */ function yarpp_custom_wp_query($args) { $meta_query = array( array( 'key' => 'sklad_1', 'value' => 0, 'compare' => '>', ) ); $args = [ 'post_status' => 'publish', 'order' => $args['order'], 'orderby' => $args['orderby'], 'p' => $args['p'], 'post_type' => $args['post_type'], 'showposts' => 6, 'meta_query' => $meta_query, ]; return $args; } add_filter( 'yarpp_wp_query', 'yarpp_custom_wp_query');
Then, in the Admin panel in the Plugin settings, I set the number of similar posts to 12 (so that at least 6 of them meet the requirement to be available on stock).
I get it! ??
And finally, why I wrote this post. Please add a filter for $wp_query to the display_related() function in future releases so that user can modify the $wp_query without changing the plugin’s files.
Thank you!
- This topic was modified 6 years, 6 months ago by . Reason: remove notes
- This topic was modified 6 years, 6 months ago by . Reason: fix some text error
The page I need help with: [log in to see the link]
- The topic ‘The query filter’ is closed to new replies.