Visual composer custom query - excluding meta_key - php

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));

Related

Combine Two PHP/MySQL queries

I'd like to combine two php/mysql queries against the wp_posts table using the code below.
However, as you can see, there's an error there with the second post_type. The second post_type is 'post', and is never recognized. :-(
How would I make that an "AND", to gather BOTH the 'membercontent' and 'post' data ? The same question applies to the 'value' statement just below it. 'true' and 'yes'.
<?php $args = array(
'post_type' => 'membercontent', 'post',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true', 'yes',
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
`$ourposts = new WP_Query( $args );?>
Just figured it out. Duh. Had the answer all along....ARRAYS!
<?php $args = array(
'post_type' => array ('membercontent', 'post',),
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => array ('true', 'yes',),
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

Find post of author with meta_key - Wordpress

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

Show pages and posts with 'true' checkbox value and sort 'em

not much add to the subj... I know how to do the first part:
$args = array('post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_key' => 'show_on_main',
'meta_value' => true);
And the second part of the task should be something like this:
$args = array ('post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_key' => 'sorting_on_main',
'orderby' => meta_value_num,
'order' => ASC);
But how can I do it both at the same time? I tried to do it with array in 'meta_key' value, but it didn't work out
$args = array(
'post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_query' => array(
array(
'key' => 'show_on_main',
'value' => '1',
'compare' => '=='
)
),
'meta_key' => 'sorting_on_main',
'orderby' => meta_value_num,
'order' => 'ASC'
);

WordPress Loop Not Respecting Post Status

I have a series of loops spitting out 'event' custom post types that I want to only pull posts with a post_status of 'publish'. I've added the post_status of 'publish' in my wp_query array but it doesn't seem to work. I still have scheduled posts showing up.
<?php
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => '_expiration_date',
'orderby' => 'meta_value',
'order' => 'ASC',
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
$wp_query4 = new WP_Query($args_hotel_feature);
if($wp_query4->have_posts()) :
while($wp_query4->have_posts()) :
$wp_query4->the_post();
?>
Anyone else experienced this issue?
Use meta_query
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
)
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
Turns out the query was fine, however, the post expiration plug-in "Posts Expiration Date" breaks post_staus. Use "Post Expirator" instead.

Categories