Title: Post filter
Last modified: November 2, 2018

---

# Post filter

 *  [valery2016](https://wordpress.org/support/users/valery2016/)
 * (@valery2016)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/)
 * Hi All!
    I am not a native speaker of English, sorry.
 * How to organize Records filtering by metabox field values?
    I created a custom
   post. Custom taxonomy allows you to get the archives of posts related to a specific
   term. I want in each archive I receive (for each taxonomy term) to organize a
   filter of the displayed Posts by the values of the metabox fields.
 * For ex.: [image](https://res.cloudinary.com/valery/image/upload/v1541137933/customArchive_w2tzjl.jpg)
   
   This is an archive page – taxonomy-term-one.php.
 * Is it possible?
 * Thanks!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fpost-filter-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10841987)
 * Yes! The filter you want is actually an action. They are closely related, but
   you interact with the passed data differently. The action is called “[pre_get_posts](https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts)”
   and your callback is passed the current WP_Query class object by reference. ALL
   post queries pass through this action, so you need to be sure you are affecting
   the right objects. A couple things to check is `$query->is_main_query()` and `!
   $query->is_admin()`, and perhaps `$query->is_archive()`, plus anything else, 
   such as the post type and your taxonomy exist as query vars gotten with `$query-
   >get()`. I’m assuming your callback collects the passed object with `$query`.
 * Once you are sure you are changing the correct query, set query var (with `$query-
   >set()`) “orderby” to “meta_value” and set “meta_key” to your meta value’s key
   name. For good measure, set “order” to “ASC” since the default is “DESC”.
 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10841991)
 * You can use the `pre_get_posts` action hook ([Codex](https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts),
   [Developer Resources](https://developer.wordpress.org/reference/hooks/pre_get_posts/))
 * An example would be:
 *     ```
       /**
        * Modify the post query before it gets requested
        *
        * @param WP_Query $query
        *
        * @return void
        */
       function wpf10840294_modify_query( $query ) {
   
       	// Prevent running this on admin ( optional )
       	if( is_admin() ) {
       		return;
       	}
   
       	// IF the query is the main loop on the page ( not a secondary WP_Query )
       	if( $query->is_main_query() {
   
       		/**
       		 * IF 	we're currently viewing a taxonomy archive page
       		 * AND 	the viewed taxonomy is "taxonomy" and the viewed term is "term-slug"
       		 *
       		 * if you only want this to run on one term, 
       		 * you would replcae "term-slug" with the slug of the term you would run it on
       		 * and the "taxonmy" with the taxonomy the term belongs to.
       		 * 
       		 * if you want it to apply to all terms of the taxonomy, remove the 2nd parameter
       		 * and replace "taxonomy" with the desired taxonomy.
       		 */
       		if( $query->is_tax() && $query->is_tax( 'taxonomy', 'term-slug' ) {
   
       			/**
       			 * Query the meta value by key name
       			 *
       			 * @see https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
       			 */
       			$query->set( 'meta_key', 'meta_key_name' );
   
       			/**
       			 * Order by the meta value
       			 * if the value is a numeric value use "meta_value_num"
       			 *
       			 * @see https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
       			 */
       			 $query->set( 'orderby' => array( 'meta_value' => 'ASC'  ) );
   
       		}
   
       	}
   
       }
       add_action( 'pre_get_posts', 'wpf10840294_modify_query' );
       ```
   
 * One catch is that every post needs to have the meta key or it will be excluded
   from the query. The above code could go in a child themes `functions.php` file
   or a plugin file.
 * Let me know if you have any further questions!
 *  Thread Starter [valery2016](https://wordpress.org/support/users/valery2016/)
 * (@valery2016)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10842125)
 * **bcworkz**,
    **Howdy_McGee**
 * Thank you so much!!!
 * I will now try to implement what I have planned…
    On the results (or lack thereof)
   I will inform you
 * Thanks!
 *  Thread Starter [valery2016](https://wordpress.org/support/users/valery2016/)
 * (@valery2016)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10854250)
 * Hi All!
    The hook works great. I figured it out.
 * Now I just don’t understand how to create “controls” (filtering by color in the
   image in the starting post).
    So far I have created a form in which, when selecting
   a checkbox (color – green, for example), the line `/?option-color=green` is added
   to the URL. This is static. How to get all the values of all metabox fields dynamically(
   in WordPress) I do not know. In general, I need help …
 * If there is any tutorials on this topic, please share the link.
    Thanks!
 * Sorry for Google English 🙂
    -  This reply was modified 7 years, 7 months ago by [valery2016](https://wordpress.org/support/users/valery2016/).
 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10857241)
 * In your `pre_get_posts` hook you can look for `$_GET['option-color']` which will
   be whatever `option-color` equals in the url. In this case:
 * `$color = $_GET['option-color'] // green`
    `$query->set( 'meta_value', $color);`

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

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

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [metabox](https://wordpress.org/support/topic-tag/metabox/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/post-filter-2/#post-10857241)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
