Computing Magazine

WordPress Query Posts My Meta Data

Posted on the 27 April 2017 by Akahgy

Description:

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.

Solution:

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

$args = array(
‘post_type’ => ‘post’,
‘meta_query’ => array(
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.

Advertisements

Back to Featured Articles on Logo Paperblog

Magazine