How to pass custom parameter to WP_Query for filtering in pre_get_posts

You can access query variables (including custom ones) via the WP_Query::get() method. For example: $my_query = new WP_Query( array( … ‘wpse105219_custom_var’ => ‘foobar’, … ) ); To ‘catch’ this at pre_get_posts: add_action( ‘pre_get_posts’, ‘wpse105219_pre_get_posts’, 10 ); function wpse105219_pre_get_posts( $query ){ if( $query->get( ‘wpse105219_custom_var’ ) == ‘foobar’ ){ //Alter $query } }

Help With issue on pre_get_posts filter in taxonomy

Solved! I was using the wrong technique to create a custom query to my taxonomy instead of using the function above I create new function that use query_vars. Here is the code function taxonomy_posts_order($query) { global $browsetype; $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) ); if ( $query->query_vars[‘taxonomy’] == $term->slug ) { … Read more

Create multiple Search functions for posts / custom post types and everything

you can change your search filter to this: function SearchFilter($query) { $post_type = $_GET[‘post_type’]; if (!$post_type) { $post_type=”any”; } if ($query->is_search) { $query->set(‘post_type’, $post_type); }; return $query; } add_filter(‘pre_get_posts’,’SearchFilter’); then on your news search form add : <input type=”hidden” name=”post_type” value=”post” /> on your custom post type listing search form add <input type=”hidden” name=”post_type” value=”customtypename” … Read more

Meta Query “IN” doesn’t work with ACF checkbox filter

Checkbox field is stored as serialized array, therefore you can not use the IN operator and array with the values you are looking for. To get posts with checked “melbourne”, change meta_query to: $meta_query = array( array( ‘key’ => $name, ‘value’ => ‘”melbourne”‘, ‘compare’ => ‘LIKE’, ) ); To get posts with melbourne or sydney: … Read more

How to alter query order direction using $query->set(‘order’, ‘ASC’); inside a pre_get_posts filter?

Yes, I got it: function custom_search_orderby($orderby) { global $wpdb; if ( is_search() ) { if ( isset($_GET[‘order’]) ) { if ( $_GET[‘order’] == ‘DESC’ ) { $orderby = $wpdb->prefix . “posts.post_date DESC”; } else { $orderby = $wpdb->prefix . “posts.post_date ASC”; } } } return $orderby; } add_filter(‘posts_orderby’, ‘custom_search_orderby’, 999);

Order posts by (hierarchical custom) taxonomy terms and term children

This query will handle two levels of hierarchy in your taxonomy. More than two levels of hierarchy and you’ll need a recursive self-join. What this does is return the posts in the correct child within parent order. To create the appropriate parent level headings, you’ll have compare the current post’s parent taxon with that of … Read more

Sort custom-posts in archive.php via meta-key?

Just use the appropriate conditionals: function change_order_for_events( $query ) { //only show future events and events in the last 24hours $yesterday = current_time(‘timestamp’) – 24*60*60; if ( $query->is_main_query() && (is_tax(‘event_type’) || is_post_type_archive(‘wr_event’)) ) { $query->set( ‘meta_key’, ‘_wr_event_date’ ); $query->set( ‘orderby’, ‘meta_value_num’ ); $query->set( ‘order’, ‘ASC’ ); //Get events after 24 hours ago $query->set( ‘meta_value’, $yesterday … Read more