How can I get_posts with pages using several page templates? - php

I'm using get_posts() to return an array of pages all using a specific page template, but can I adjust so this it also pulls in pages using another (or several) page templates?
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-procedure-minimal.php'
)
)
));
Can I simple turn value into a nested array like this?
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => array(
'template-procedure.php',
'template-procedure-minimal.php'
)
)
)
));

You can use the following to look for it in an array of templates
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => array ( 'template-procedure.php', 'template-procedure-minimal.php' ),
'compare' => 'IN'
)
)
);

Related

wordpress new WP_Query between not working

How can I make it filter between the two integers?
This is my PHP
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'), //already tried to remove the qoutes on numbers
'compare' => 'BETWEEN'
)
);
$posts = new WP_Query($filter);
print_r($posts);
The results of print_r it displaying the outside of the filtered of two integers. Where I have gone wrong?
Multiple meta values can be compared with BETWEEN with an array value:
'meta_query' => array(
array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
You can see this in Codex
Try it:
//Date
$start = '2017-08-26';
$end = '2017-08-29';
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'first_posting_date',
'meta_query' => array(
array(
'key' => 'first_posting_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
)
);
// Make the query
$posts = new WP_Query($filter);
print_r($posts);
The answer to my question is relation and put the array of meta_query to other array
so it would be like this
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
"relation" => "AND",
array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'),
'compare' => 'BETWEEN'
)
)
);
$posts = new WP_Query($filter);
print_r($posts);

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

Visual composer custom query - excluding meta_key

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

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

meta_query not working in wp_query

My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:
$the_query = new WP_Query(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
);
It doesn't show any post.
I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.
WP_query needs to be passed in an array. use following code and let me know if any prob.
$the_query = new WP_Query (array (
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
You can refer to the discussion at wordpress forum:
http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string
You're passing all of this into WP_Query as individual arguments when they should be contained in an array.
$the_query = new WP_Query( array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN',
),
),
) );
Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.
Are you sure there is no php error?
I think WP_Query needs to be passed in an Array
$the_query = new WP_Query(
array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...

Categories