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?

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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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