Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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

if($my_query->have_posts()):
    while($my_query->have_posts()):$my_query->the_post();
        //All the post stuff here.
    endwhile;
endif;
wp_reset_postdata();

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

 
discard

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.