WordPress query posts my meta data

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

About admin

Just another php developer trying to give something back to the community.
This entry was posted in Wordpress and tagged , , . Bookmark the permalink.

One Response to WordPress query posts my meta data

  1. Pingback: WordPress proper query_posts with real results | justbugs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s