0

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?

1 Answer 1

3

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.

3
  • 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. Commented Oct 1, 2011 at 4:38
  • Update.. I was able to make work by adding a comma at the end of meta_value line... Thank you! Commented Oct 1, 2011 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! Commented Oct 1, 2011 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.