Title: The query filter
Last modified: July 22, 2019

---

# The query filter

 *  [Mikhail Alferov](https://wordpress.org/support/users/malferov/)
 * (@malferov)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/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 7 years, 6 months ago by [Mikhail Alferov](https://wordpress.org/support/users/malferov/).
      Reason: remove notes
    -  This topic was modified 7 years, 6 months ago by [Mikhail Alferov](https://wordpress.org/support/users/malferov/).
      Reason: fix some text error
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fthe-query-filter%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Support [Michael Nelson](https://wordpress.org/support/users/mnelson4/)
 * (@mnelson4)
 * [7 years ago](https://wordpress.org/support/topic/the-query-filter/#post-11311249)
 * Hey [@malferov](https://wordpress.org/support/users/malferov/) that’s probably
   a good idea. Wanna make a pull request for [https://github.com/mnelson4/yet-another-related-posts-plugin](https://github.com/mnelson4/yet-another-related-posts-plugin),
   I’ve forked the plugin and am working on getting the fork onto WordPress.org
 *  Thread Starter [Mikhail Alferov](https://wordpress.org/support/users/malferov/)
 * (@malferov)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11738896)
 * Hey [@mnelson4](https://wordpress.org/support/users/mnelson4/), is there any 
   success in adding a filter to the query (function display_related() in YARPP_Core.
   php)?
 * Probably, it is possible to catch a request from YARPP through the hook ‘pre_get_posts’,
   but, as it seems to me, adding a filter to the YARPP core would give a faster
   and more reliable result.
 *  Plugin Support [Michael Nelson](https://wordpress.org/support/users/mnelson4/)
 * (@mnelson4)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11741015)
 * Hi thanks for pinging this topic [@malferov](https://wordpress.org/support/users/malferov/).
   It would probably be best if [@yarpp](https://wordpress.org/support/users/yarpp/)
   addressed your request in the WordPress.org repo version.
    If they don’t reply,
   can you make a pull request on my GitHub repo (linked-to above)?
 *  Plugin Author [YARPP](https://wordpress.org/support/users/jeffparker/)
 * (@jeffparker)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11741294)
 * [@malferov](https://wordpress.org/support/users/malferov/) If you can create 
   a PR on [@mnelson4](https://wordpress.org/support/users/mnelson4/) repo for this,
   we’ll use it as a template to include it properly in the main distribution.
 * ps. I know this is not an ideal way to accept community contributions. Need to
   think through a better way to manage this…
 *  Thread Starter [Mikhail Alferov](https://wordpress.org/support/users/malferov/)
 * (@malferov)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11743223)
 * [@jeffparker](https://wordpress.org/support/users/jeffparker/) This is the first
   time I send a Pull request to Github and I think I’ve got it.
 * 1. Changed the YARPP_Core.php and clicked “Propose file change”. The project 
   was automatically cloned into a separate fork in my profile.
 * 2. I pressed “Compare and Pull request”, and saw that the profile [@mnelson4](https://wordpress.org/support/users/mnelson4/)
   tab Pull requests appeared my request 🙂
 *  Plugin Author [YARPP](https://wordpress.org/support/users/jeffparker/)
 * (@jeffparker)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11751798)
 * [@malferov](https://wordpress.org/support/users/malferov/) can you please provide
   the Github link to the Pull Request? Many thanks!
 *  Thread Starter [Mikhail Alferov](https://wordpress.org/support/users/malferov/)
 * (@malferov)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11752086)
 * [@jeffparker](https://wordpress.org/support/users/jeffparker/) i think that’s
   it: [https://github.com/mnelson4/yet-another-related-posts-plugin/pull/13](https://github.com/mnelson4/yet-another-related-posts-plugin/pull/13)

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘The query filter’ is closed to new replies.

 * ![](https://ps.w.org/yet-another-related-posts-plugin/assets/icon-256x256.png?
   rev=2549977)
 * [YARPP - Yet Another Related Posts Plugin](https://wordpress.org/plugins/yet-another-related-posts-plugin/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/yet-another-related-posts-plugin/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/yet-another-related-posts-plugin/)
 * [Active Topics](https://wordpress.org/support/plugin/yet-another-related-posts-plugin/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/yet-another-related-posts-plugin/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/yet-another-related-posts-plugin/reviews/)

## Tags

 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)
 * [YARPP](https://wordpress.org/support/topic-tag/yarpp/)

 * 7 replies
 * 3 participants
 * Last reply from: [Mikhail Alferov](https://wordpress.org/support/users/malferov/)
 * Last activity: [6 years, 8 months ago](https://wordpress.org/support/topic/the-query-filter/#post-11752086)
 * Status: not resolved