WordPress.org

Ready to get started?Download WordPress

Forums

Get Posts That Have A Specific Custom Field Key (9 posts)

  1. alanchrishughes
    Member
    Posted 3 years ago #

    I have band profile pages and a custom field for each page with their up coming gigs.

    For the front page of the site I want to pull a feed of just the bands that currently have upcoming gigs entered in their custom field.

    I've tried a bazillion different tutorials and things listed in the codex, but nothing seems to be working. Any ideas? Thank you ahead of time, I appreciate any help.

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

  3. kkarpieszuk
    Member
    Posted 3 years ago #

    i dont if you exists ready function for this. but you can query posts directly from database

    first check ids of posts which has meta key:

    "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = 'your meta key here'"

    then query posts with those ids which you selected above

  4. alanchrishughes
    Member
    Posted 3 years ago #

    I've tried quite a few variations from that codex page and other alchymth, but I must be missing something still. I've tried

    <?php query_posts('post_type=page&meta_key=gigONE'); ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    THREE
    <div class="BandListItem">
    
    <?php the_title(); ?>
    
    <?php meta('gigONE'); ?>
    
    </div> 
    
    <?php endwhile; ?>
    
    <?php endif; ?>
  5. alanchrishughes
    Member
    Posted 3 years ago #

    <?php query_posts('meta_key=review_type&meta_value=gigONE');  ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    TWO
    <div class="BandListItem">
    
    <?php the_title(); ?>
    
    <?php meta('gigONE'); ?>
    
    </div> 
    
    <?php endwhile; ?>
    
    <?php endif; ?>
  6. alanchrishughes
    Member
    Posted 3 years ago #

    <div class="AlphaBandListContainer">
    ONE
    
    <?php $loop = new WP_Query( array( 'post_type' => 'band', 'orderby' => 'title', 'order' => 'asc', 'posts_per_page' => '10', true ) ); ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <div class="BandListItem">
    
    <?php the_title(); ?>
    
    <?php meta('gigONE'); ?>
    
    </div>  
    
    <?php endwhile; ?>
    
    </div>
  7. alanchrishughes
    Member
    Posted 3 years ago #

    <?php query_posts('post_type=page&meta_key=gigONE'); ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <div class="BandListItem">
    
    <?php the_title(); ?>
    
    <?php meta('gigONE'); ?>
    
    </div><!-- BandListItem -->
    
    <?php endwhile; ?>
  8. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    i guess, the output is 'empty' with all these attempts?

    if you are not clear if you have post_type=page or post_type=band, try with post_type=any:

    <?php query_posts('post_type=any&meta_key=gigONE'); ?>

  9. alanchrishughes
    Member
    Posted 3 years ago #

    These results have either empty or they bring back everything.

    They are pages, not posts, and the custom field key is gigONE. So I'm not entirely sure when I should use one or the other in the code.

    I'm not sure if I made it clear in my original post, but I am only trying to pull pages that have something entered in this custom field, so I am thinking I need to something to specify that also, something to say pull pages if this meta key (gigOne) exists and only if there is a value entered.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.