WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to filter a loop by a custom field value. I've tried all of the suggestions on the site, but it just won't filter. This loop is being used for a custom template of an rss feed. It pulls together the custom post type and only the posts that are "published", but I can't get it to filter by custom field value.

global $wp_query; $args = array_merge( $wp_query->query, array( 'post_type' => 'custom_type', 'post_status' => 'publish', 'meta_key' => 'my_custom_field', 'meta_value' => 'custom field value', ) ); query_posts( $args ); ?>

while( have_posts()) : the_post(); ?>

Any suggestions?

share|improve this question
up vote 3 down vote accepted

How about this:

$my_query = new WP_Query(array(
                                'post_type'=> 'custom_type',
                                'post_status' => 'publish',
                                'meta_key' => 'my_custom_field',
                                'meta_value' => 'custom field value'

        //All the post stuff here.

Check this out.

share|improve this answer
Thanks for the suggestion. The loop works fine with the post type and post status, it just doesn't filter the loop by meta key and meta value. – user7481 Oct 1 '11 at 4:38
Update.. I was able to make work by adding a comma at the end of meta_value line... Thank you! – user7481 Oct 1 '11 at 4:54
Glad it worked! You're welcome! But I don't understand how that associative array worked with a 'comma' after the last key-value pair, that's not supposed to happen! Not an issue though! – Rutwick Gangurde Oct 1 '11 at 5:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.