Adding a html class based on post count

If I understood this issue correctly the problem with your current code is that you are incrementing $totalPosts variable inside the while loop, so the variable gets incremented after every post thus resulting in the running order you described. To get the amount of posts returned matching the query, use $loop->found_posts (in this case) More … Read more

Wp_query with 2 meta keys and array of meta values

I think you need to do something like this: <?php $args = array( ‘post_type’ => ‘shop’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘cu_status’, ‘value’ => array( ‘pending’, ‘processing’ ), ‘compare’ => ‘IN’, ), array( ‘key’ => ‘cu_date’, ‘value’ => array( ‘2016-01-12’, ‘2016-01-13’ ), ‘compare’ => ‘BETWEEN’, … Read more

wp_query search not taking keywords with together for multiple words

WordPress also provide more filter to can change wp query using filters. What i need logical to use filter to override query below sample of code if this helpful for you. add_filter( ‘posts_where’, ‘posts_where_condition_search’ ); $wp_query = new WP_Query( array( ‘post_type’ => ‘press-release’, ‘posts_per_page’ => -1, ) ); remove_filter( ‘posts_where’, ‘posts_where_condition_search’ ); function posts_where_condition_search( $where … Read more

wp query error while paging the posts

You are setting null to $wp_query then querying get_query_var( ‘paged’ ) this is expected that you will get this error! First query get_query_var( ‘paged’ ) then set the original $wp_query to null Example:- $paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1; global $wp_query; $temp = $wp_query; $wp_query = new WP_Query( … Read more

How to provide meta_key array to wp_query?

The WP_Query() custom field (i.e. meta) query can handle arrays for field values. You just need to add the compare key to your array: $args = array( ‘numberposts’ => -1, ‘post_type’ => ‘post’, ‘meta_query’ => array ( array ( ‘key’ => ‘my_key’, ‘value’ => ‘target_value’, ‘compare’ => ‘IN’ ) ) ); $new_query = new WP_Query( … Read more

Exclude current post from an array of posts?

Use array_diff() to remove the current post ID from an array of other post IDs. $include = array( 17, 111, 108, 158); $include = array_diff( $include, array( $post->ID ) ); $args = array( ‘post__in’ => $include ); $posts = new WP_Query( $args ); Edit to include a point raised by Pieter Goosen–If $include is not … Read more

Order or Orderby in tax_query (How to define order of terms in WP_Query)

From the get_terms documentation: orderby (string): Field(s) to order terms by. Accepts term fields (‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’), ‘count’ for term taxonomy count, ‘include’ to match the ‘order’ of the $include param, ‘meta_value’, ‘meta_value_num’, the value of $meta_key, the array keys of $meta_query, or ‘none’ to omit the ORDER BY clause. Defaults to … Read more