Post filter
-
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
This is an archive page – taxonomy-term-one.php.Is it possible?
Thanks!
The page I need help with: [log in to see the link]
-
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” 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”.You can use the
pre_get_posts
action hook (Codex, Developer Resources)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!
bcworkz,
Howdy_McGeeThank you so much!!!
I will now try to implement what I have planned…
On the results (or lack thereof) I will inform youThanks!
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 5 years, 5 months ago by valery2016.
In your
pre_get_posts
hook you can look for$_GET['option-color']
which will be whateveroption-color
equals in the url. In this case:$color = $_GET['option-color'] // green
$query->set( 'meta_value', $color );
- The topic ‘Post filter’ is closed to new replies.