Why is my WP_Query not working when tax_query terms are an array?

When you’re doing a tax_query or meta_query in a WP_Query, you always have to use a nested array( array() ); just see the following example for an explanation and pay attention to the relation argument. $packages = new WP_Query( array( ‘post_type’ => ‘vamos-cpt-packages’, ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘vamos-holiday-types’, ‘field’ => … Read more

WP_Query with meta_value LIKE ‘something%’

For a left-sided match you can get around the automatic adding of ‘%’‘s by WP by using regular expression RLIKE: ‘meta_value’ => ‘^’ . preg_quote( $today ), ‘meta_compare’ => ‘RLIKE’ Similarly for right-sided match: ‘meta_value’ => preg_quote( $today ) . ‘$’, ‘meta_compare’ => ‘RLIKE’

Getting an array out of WPQuery

Your function returns $product->get_id();, instead of that, you should save those values into an array and at the end return that array. function ids(){ $args = array( ‘numberposts’ => -1, ‘post_type’ => ‘product’, ‘meta_key’ => ‘wppl_is_dax’, ‘meta_value’ => ‘1’ ); // query $the_query = new WP_Query( $args ); $allIds = array(); if( $the_query->have_posts() ): while( … Read more

How to limit the posts

It should be: $newsposts = new WP_Query(‘cat=restaurant&posts_per_page=7’); Another way to write it (helps readability with larger queries) would be: $newsposts = new WP_Query(array( ‘cat’ => ‘restaurant’, ‘posts_per_page’ => 7, )); See WP_Query in Codex for description of available parameters. PS would be good practice to add wp_reset_postdata() at the end. You are (correctly) not modifying … Read more

Display only posts with thumbnail using WP_Query

You need to define your arguments before you pass them to WP_Query, not after. Also, your meta_query should be an array of an array, not just an array This $query = new WP_Query($thumbs); $thumbs = array( ‘meta_query’ => array(‘key’ => ‘_thumbnail_id’) ); should look like this $thumbs = array( ‘meta_query’ => array( array( ‘key’ => … Read more

How to trigger 404 for custom query var?

There is an action specifically for this: function my_parse_query( $wp_query ) { if ( $wp_query->get( ‘my_custom_var’ ) > 42 ) { $wp_query->set_404(); status_header( 404 ); } } add_action( ‘parse_query’, ‘my_parse_query’ ); That should load the 404.php template in your theme, if you have it. Otherwise, it will fall back to index.php. This will also trigger … Read more

modifying meta_query on parse_query

Right after the pre_get_posts hook is fired, the public meta_query attribute of WP_Query is overridden with: $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $q ); where $q = &$this->query_vars; $q = $this->fill_query_vars($q); So I don’t think it will work to modify this attribute, like you’re trying to do, before the pre_get_posts hook is activated. Instead we need … Read more

“pre_get_posts” firing on every query

Basically what you are looking for is the global $wp_the_query variable which is set to the value of the main query. It may not be a perfect fit for 100% of cases but will probably work fine in 99% of cases: add_action( ‘pre_get_posts’, ‘custom_post_count’ ); function custom_post_count( $query ){ global $wp_the_query; if ( $wp_the_query === … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)