WP_Query no posts with tax_query

Your query can be simplified to this: $products = new WP_Query([ ‘post_type’ => ‘post’, ‘show_product_on_only_premium’ => ‘yes’, ]); echo var_dump($products->posts); The culprit was likely the ‘operator’=>’AND’, the default value is IN, so changing your original query to use IN instead of AND would also work, as would removing the operator parameter as the default is … Read more

How to get posts on a specific date – WP Query

<?php // primarily get $year, $month and $day $args = array( ‘date_query’ => array( ‘year’ => $year, ‘month’ => $month, ‘day’ => $day, ) ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { // do whatever here } }

Get ONLY the count from wp_query without fetching posts

The found_posts property of the WP_Query class is what you’re looking for, which is the amount of found posts for the current query. However, you got a 0 because you set no_found_rows (in the $query array) to true, which means the amount there will not be calculated. I.e. WordPress will not append SQL_CALC_FOUND_ROWS to the … Read more

Pass array of taxonomy terms to wp_query

The problem is ambiguity, you can’t just put in ‘relation’ => ‘OR’ in the main parameter list, because how would WP_Query know if it’s for the tax_query and not the meta_query? For how it’s supposed to work, and where the relation parameter goes we need to refer to the official docs for WP_Query which gives … Read more

wp_query to find posts by year and month

Solved by creating custom sql queries. Request for years: SELECT count(*), $wpdb->posts.id as post_id_ts, YEAR( (SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = post_id_ts AND $wpdb->postmeta.meta_key = ‘docs_pubdate’)) AS years FROM $wpdb->posts WHERE $wpdb->posts.post_type=”document_base” AND $wpdb->posts.post_status=”publish” GROUP BY years which will return result like this: Array ( [0] => Array ( [count(*)] => 1 [post_id_ts] => … Read more

$wpdb insert is not work

First insert the global. global $wpdb; Then You have to check your quotes properly. $sql = $wpdb->prepare( “INSERT INTO `$table_name` (`name`,`email`,`phone`,`message`,`rq`,`url`) values ($name, $email, $phone, $address, $msg, 1, $url)”); $wpdb->query($sql); OR $wpdb->insert(‘$table_name’, array( ‘name’ => $name, ’email’ => $email, ‘phone’ => $phone, // … and so on ));

tax_query returning all posts instead of selective posts in WP_Query

Taxonomy Parameters Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays. In ‘tax_query’ you should add array of arrays (see … Read more

Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX

Because of this line: $paged = get_query_var( “paged” ) ? get_query_var( “paged” ) : 1; This is inside a function that is used on both an AJAX handler, and on a page request. get_query_var pulls the parameters from the main query ( aka the primary WP_Query that powers functions such as the_post, have_posts() etc ). … Read more