WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] post_meta in WP_Query (6 posts)

  1. christhesoul
    Member
    Posted 4 years ago #

    Hi

    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

    <?php
    $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
    Member
    Posted 4 years ago #

    This works okay for me so don't know why your example doesn't work:

    <?php
        $args=array(
          '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>
           <?php
          endwhile;
        }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  4. christhesoul
    Member
    Posted 4 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
    Member
    Posted 4 years ago #

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

  6. radiofranky
    Member
    Posted 4 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'?

    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic