Computing Magazine

WordPress Query Posts My Meta Data

Posted on the 27 April 2017 by Akahgy


On a WP powered site, if you add custom meta information, you might want to query the posts that have a particular meta information, like a custom post template.


This is done very easy and safe using the WP_Query() function with following arguments:

$args = array(
‘post_type’ => ‘post’,
‘meta_query’ => array(
‘key’ => ‘custom_post_template’,
‘value’ => ‘templatename.php’
$queryPosts = new WP_Query( $args );

while ($queryPosts->have_posts()) {
//do something with each post

In the while() loop you can do anything with the post object.


Back to Featured Articles on Logo Paperblog