Support » Fixing WordPress » post_meta in WP_Query

  • Resolved christhesoul



    I was wondering whether I could filter to show only posts with a custom field of, for instance, “next” (as the key, the value doesn’t really matter at the moment.)

    I’ve tried

    $my_query = new WP_Query('meta_key=next');
    while ($my_query->have_posts()) : $my_query->the_post();
    do stuff
    <?php endwhile; ?></div>

    Can somebody explain why this doesn’t work and how, perhaps, I can achieve what I’m trying to do?

    I guess it’s the argument I’m putting in ‘meta_key=next’ as ‘page_id=8’, for instance, works fine.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • This works okay for me so don’t know why your example doesn’t work:

          'meta_key' => 'cf1',
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts with cf1 custom field';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    wp_reset_query();  // Restore global post data stomped by the_post().

    Works fine for me to, would need to see more of your code..

    Really appreciate your help, guys.

    Creating an array and adding the ‘post_type’ => ‘page’ seems to make it work. Does it default to only posts?

    Anyway. It’s working. Much appreciated.

    Yes the ‘default’ post_type, if not specified is “post”.

    ‘meta_key’ => ‘cf1’ <= custom field?

    is the key or the value of the key?

    for example, I have a custom field called “video”
    and value “HD” or “standard”

    if I want to list videos with “HD” I would lput ‘meta_key’ => ‘HD’?


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘post_meta in WP_Query’ is closed to new replies.