I see how to do multiple meta_keys or multiple category_names, but how do I combine them?
$args = array(
'post_type' => 'quotes',
'meta_key' => 'newsletter',
'meta_value' => '1',
'category_name' => 'dogs',
);
$query1 = new WP_Query( $args );
You can use tax_query and meta_query.
$args = array(
'post_type' => 'quotes',
'meta_key' => 'newsletter',
'meta_value' => '1',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => 'bob',
)
),
'meta_query' => array(
array(
'key' => 'newsletter',
'value' => 1,
'compare' => 'LIKE'
)
),
);
$query1 = new WP_Query( $args );
Related
I have 3 products having meta key 'check_key' = 1. 2 of them have attribute 'pa_size_10_20' with values '100' and '30'; other one have attribute 'pa_size_30_40' with value '70'.
Please help me understand why the query below returns all 3 products? I expect only 2 products with 'pa_size_10_20' attribute.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => 0,
'operator' => '!='
)
)
);
$posts = get_posts( $args );
TIA
It seems as though your tax_query would return all taxonomy terms that aren't named 0 an integer?
I think if you know the values for IN then I would use those instead.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
),
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => array('100', '300'),
'operator' => 'IN'
),
),
);
This is the code I am using:
$results = array(
'post_type' => 'score',
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'numberposts' => -1,
'post_type' => 'score',
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query($results);
The problem is I can't work out how to make a Boolean AND expression so that both the taxonomy and the custom field should match before returning a post.
I revised your code. check below code.
$results = array(
'post_type' => 'score',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $results );
I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as yes.
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
Any help would be appreciated.
Thanks
There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here, but it's not working either.
//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();
$args_exclude = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
),
),
);
$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
foreach ($exclude_posts->posts as $post)
{
$exclude_id[] = $post->ID;
}
}
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);
// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
print_r($post);
}
Hope this helps!
See: visual composer wordpress query for post grid
Try this:
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
echo http_build_query($args);
// Result:
post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS
http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e
I would recommend to avoid use array('post') in case if only one post type, so just use post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS
P.S.
possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));
I'm trying to show the number of posts that have certain meta key values for the current user.
This is my code:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
'key' => 'color',
'value' => array('red', 'blue')
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
echo $the_count;
Thi is counting ALL posts for the current user, ignoring the meta key values.
I only need the $the_count to be the number of posts that have a meta key value 'red' or 'blue' for the current user.
What am I doing wrong?
Thanks in advance!
I am not sure, but you could try something like this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_key' => 'color',
'meta_value' => array('red', 'blue')
);
$posts_query = new WP_Query($args);
$the_count = $posts_query->post_count;
echo $the_count;
If you want to use the meta_query array, you have to put the meta_key and meta_value in a subarray:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
This is because you can use multiple meta_key to combine them.
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
array(
'key' => 'size',
'value' => array('l', 'xl', 'xxl'),
),
),
This's my query:
$list = query_posts(array(
'post_type' => 'post',
'author' => $current_user->ID,
'category__in' => array(11),
));
It ok, but when I change it to:
$list = query_posts(array(
'post_type' => 'post',
'author' => $current_user->ID,
'category__in' => array(11),
'meta_key' => 'author_alias_id',
'meta_value' => '1'
));
The result is empty.
Somebody can help me?
$args = array(
'post_type' => 'my_custom_post_type',
'meta_key' => 'age',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'age',
'value' => array( 3, 4 ),
'compare' => 'IN',
),
),
);
Refer this example