Creating arguments from loop for WP_Query meta_query

How about this? /** * Convert query string to meta query. */ function query_string_to_meta_query( $query_string ) { $meta_query = array( ‘relation’ => ‘AND’ ); foreach ( wp_parse_args( $query_string ) as $key => $value ) { $meta_query[] = array( ‘key’ => $key, ‘value’ => $value ); } return array( ‘post_type’ => array( ‘post’, ‘custom’ ), ‘meta_query’ … Read more

Querying Term Posts in Loop

To expand on the answer from @s_ha_dum – you need to modify a few things about your query. First, use his suggested tax query: $args = array( ‘post_type’ => ‘custcpt’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘custtax’, ‘field’ => ‘id’, ‘terms’ => $cat->term_id, ) ), ‘order’ => ‘ASC’ ); Then, modify your code as follows: … Read more

When to add_filter() to Custom Query

You want to add your filter before you create the query: add_filter( ‘posts_join’, ‘custom_posts_join’ ); $test = new WP_Query( $args ); where you can define the filter callback in your theme files, for example functions.php or in a plugin: function custom_posts_join( $join ) { // modifications // $join = … // remove the filter remove_filter( … Read more

Only display a certain number of posts, dependent on how many posts there are available in a query

You don’t have to run two queries, as you already got what you want (even more). $args = array ( ‘post_type’ => ‘mytheme_posttype’, ‘post_status’ => ‘publish’, ‘numberposts’ => 12, ); $sliderQuery = new WP_Query($args); $num_posts = $sliderQuery->post_count; if ($num_posts >=4) { if ($num_posts >= 12) $postsToPrint = 12; elseif ($num_posts >=8) $postsToPrint = 8; else … Read more

Adding Custom Fields to Search

1) I think that your solution is perfectly acceptable in terms of the “right way” – post meta is intended for searching. Doesn’t seem hacky to me. 2) What you should do is build the meta_query separately: $meta_query = array(); if( !empty( $_GET[‘price’] ) ) { $meta_query[] = array( ‘key’ => ‘_price’, ‘value’ => $_GET[‘price’] … Read more