[resolved] post_meta in WP_Query (6 posts)

  1. christhesoul
    Posted 6 years ago #


    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.

  2. MichaelH
    Posted 6 years ago #

    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().
  3. Mark / t31os

    Posted 6 years ago #

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

  4. christhesoul
    Posted 6 years ago #

    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.

  5. MichaelH
    Posted 6 years ago #

    Yes the 'default' post_type, if not specified is "post".

  6. radiofranky
    Posted 5 years ago #

    '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'?


Topic Closed

This topic has been closed to new replies.

About this Topic