How to prevent writing duplicate loops?

Move the control structure outside the template file and just put the repeatable markup and template tags inside. $event_query = new WP_query($args); if( $event_query->have_posts() ){ while( $event_query->have_posts() ){ $event_query->the_post(); get_template_part(‘loop’, ‘home’); } } wp_reset_postdata();

Display posts if specific country

Take a look at freegeoip.net. I used it like this with jquery, for example. $.get( “http://freegeoip.net/json/”, ‘jsonp’, function( data ) { country = data[‘country_code’]; if( country == ‘US’ ){ // show your content }; });

Empty tax_query array returns an empty array

As already hinted by @Milo, check if you have terms before appending your tax_query You can try the following: (Requires PHP 5.4+ due to short array syntax, revert to old syntax if necessary) $args = [ ‘post_type’ => ‘product’, ‘posts_per_page’ => 15, ‘paged’ => $paged, ‘post__not_in’ => $exclude, ‘s’ => $filter, ]; // Append our … Read more

Is it faster to query records using $wpdb instead of Wp_Query?

Yes, custom SQL queries are in essence faster than a custom instance of WP_Query. The disadvantage of a custom SQL query is that you are loosing a lot, like object cache, filters and actions, etc, etc, and it is also discouraged to use custom SQL if WordPress already offers that specific functionality. WP_Query does have … Read more

How would I format a query that depends on post parent taxonomy

You can still use get_posts: $parents = get_posts( array( ‘posts_per_page’ => -1, ‘post_type’ => ‘my_custom_post_type’, ‘fields’ => ‘ids’, // Just get IDs, not objects ‘tax_query’ => array( array( ‘taxonomy’ => ‘my_custom_tax’, ‘field’ => ‘slug’, ‘terms’ => array( ‘foo’, ‘bar’, ), ), ), ) ); $post_ids = get_posts( array( ‘posts_per_page’ => -1, ‘post_parent__in’ => $parents, ‘post_type’ … Read more

WP Query for variable taxonomies

tax_query requires an array of arrays, and $the_taxes is an array of arrays, you’ve already got 99% of your answer. foreach ($taxes as $tax) { $the_taxes[] = array ( ‘taxonomy’ => $tax, ‘field’ => ‘term_id’, ‘terms’ => $terms, ); } $the_taxes[‘relation’] = ‘OR’; $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => $the_taxes, );

is_search was called incorrectly

The Why part In the core is_search() function there’s a check if the global $wp_query is set: global $wp_query; if ( ! isset( $wp_query ) ) { _doing_it_wrong( __FUNCTION__, __( ‘Conditional query tags do not work before the query is run. Before then, they always return false.’ ), ‘3.1.0’ ); return false; } Note that … Read more

How to combine meta_query and post__in in WP_Query

post__in and post__not_in are mutually exclusive. Note: you cannot combine post__in and post__not_in in the same query. http://codex.wordpress.org/Class_Reference/WP_Query Either make the query with post__in and meta_query combination or make the query with post__not_in and meta_query combination. Any of them but not the union of them.

posts_per_page not working

The problem with the second query is due to the way that the parameters are passed. In the original code, two strings are being passed, which is not going to work: $the_query2 = new WP_Query( ‘cat=10’, ‘posts_per_page=1’ ); You can use an array (preferred method): $the_query2 = new WP_Query( array( ‘cat’ => ’10’, ‘posts_per_page’ => … Read more