0

This seems like an easy thing to do but I am missing something here. I have a custom post type called "recipient" and a meta check box called "yes_current" - (we help children with cancer and run events for them so we need to know who is a current recipient and who is a past recipient.) We want to display two sections on our RECIPIENT page: CURRENT RECIPIENTS and PAST RECIPIENTS.

My thought is to query the loop to get all of the custom_post_types that equal recipient, then display the featured image for all CURRENT recipients (if yes_current is ON) in the Current section, then do the same for PAST recipients in the Past section.

Thank you guys!

CODE:

<h1>CURRENT Recipients</h1>

<?php $current_loop = new WP_Query(array('post_type' => 'recipient', 'posts_per_page' => '-1', 'yes_current' => 'on'));

if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop-the_post(); 

<?php get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

<?php endwhile; endif; wp_reset_postdata(); ?>

<h1>PAST Recipients</h1>

<?php $current_loop = new WP_Query(array('post_type' => 'recipient', 'posts_per_page' => '-1', 'yes_current' => 'off'));

if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop->the_post(); 

<?php get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

<?php endwhile; endif; wp_reset_postdata(); ?>

Edit

Custom post meta code:

$meta_boxes[] = array( 'id' => 'yes_current', 'title' => 'Current Recipient', 'pages' => array( 'recipient' ), 'context' => 'side', 'priority' => 'low', 'fields' => array( array( 'name' => 'Current Recipient', 'id' => "{$prefix}yes_current", 'type' => 'checkbox', // Value can be 0 or 1 'std' => 1, ), ),
5
  • Please show your code.
    – kaiser
    Commented Nov 21, 2012 at 18:57
  • I'm curious why you just copied/pasted the code from my answer into your question? If that code works for you, just mark the answer as correct (via the checkmark); otherwise, it is more helpful to see your own current code, and an explanation of what's not working correctly with it. Commented Nov 21, 2012 at 19:10
  • I didn't. Kaiser asked me to put my code up and that's what I was doing while your answer was coming in. Neither work just yet.
    – Chris
    Commented Nov 21, 2012 at 19:16
  • Oh, never mind; we just used very similar variable names. :) Commented Nov 21, 2012 at 19:19
  • Where is $prefix defined for the custom post meta? Commented Nov 21, 2012 at 19:47

2 Answers 2

0

I would recommend executing two separate queries, each one differing from the other via a meta_query against your post custom meta key value.

$current_query_args = array(
    'post_type' => 'recipient',
    'meta_query' => array(
        array(
            'key' => 'yes_current',
            'value' => 'on',
            'compare' => '='
        )
    )
);

$current_query = new WP_Query( $current_query_args );

if ( $current_query->have_posts() ) : while ( $current_query->have_posts() ) : $current_query->the_post();

    // Loop goes here

endwhile; endif;

wp_reset_postdata();

$past_query_args = array(
    'post_type' => 'recipient',
    'meta_query' => array(
        array(
            'key' => 'yes_current',
            'value' => 'off',
            'compare' => '='
        )
    )
);

$past_query = new WP_Query( $past_query_args );

if ( $past_query->have_posts() ) : while ( $past_query->have_posts() ) : $past_query->the_post();

    // Loop goes here

endwhile; endif;

wp_reset_postdata();
11
  • Thanks Chip. Where you have "Loop goes here", I put in: the_title() just to see if anything would get returned and nothing has.
    – Chris
    Commented Nov 21, 2012 at 19:22
  • Okay, the issue is probably the meta_query. What is the exact name of the meta key, and what is the exact value for the meta value (i.e. is it Boolean true/false, is it a string 'true'/'false' or 'yes'/'no', or something else)? Commented Nov 21, 2012 at 19:24
  • Chip, it is a checkbox and it is called yes_current with a value of 0 or 1
    – Chris
    Commented Nov 21, 2012 at 19:27
  • And when you save it, what does it look like in your update_post_meta() call? Because if you're saving as Boolean true/false, then the meta_query should be correct. Commented Nov 21, 2012 at 19:30
  • Oh, your example code uses 'on' and 'off'. See updated answer. Commented Nov 21, 2012 at 19:31
0

So I figured it out. Since I am using Meta Boxes from the guys at Deluxe Blog Tips, I used their helper function that replaces get_post_meta. Here is the final code to display a list of CURRENT recipients and a list of PAST recipients for my custom_post_type Recipients. The meta box being compared to value is rec_yes_current:

<h1>Current Recipients</h1>
<?php /* Start loop */ ?>
<?php $linkposts = new WP_Query(array('post_type' => 'recipient', 'posts_per_page' => '-1'));
  while ($linkposts->have_posts()) : $linkposts->the_post(); 
  $linkvalue = $linkposts->ID;
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($linkvalue), 'post-thumbnail' );
    $medical = get_post_meta(get_the_id(), 'rec_medical', true);
$twitter = get_post_meta(get_the_id(), 'rec_twitter', true);
$facebook = get_post_meta(get_the_id(), 'rec_facebook', true);
$dob = get_post_meta(get_the_id(), 'rec_dob', true);
    $today = date("m/d/Y");
    list($m,$d,$y) = explode('/',$dob);
    list($tm,$td,$ty) = explode('/',$today);
$year=$ty-$y;
    $current = rwmb_meta( 'rec_yes_current' ); 
?>

<?php  // if is current
if($current == '') {
echo '<div class="single recipient">';
echo '<p class="recname"><a href="' . get_permalink($linkvalue) . '">' . get_the_title($linkvalue) . '</a></p>';
echo '<a href="' . get_permalink($linkvalue) . '" class="frame">' . get_the_post_thumbnail($linkvalue, 'thumbnail') . '</a>';
echo '<p class="year"><strong>Age</strong>: ' . $year . ' years old</p>';
echo '<p class="condition"><strong>Conditions</strong>:' . $medical . '</p>';       
echo '<p class="readmore"><a href="' . get_permalink($linkvalue) . '">read more</a></p>';
echo '</div>';
}; ?>

<h1>Past Recipients</h1>

<?php  // if is current
if($current != '') {
    echo '<div class="single recipient">';
    echo '<p class="recname"><a href="' . get_permalink($linkvalue) . '">' . get_the_title($linkvalue) . '</a></p>';
    echo '<a href="' . get_permalink($linkvalue) . '" class="frame">' . get_the_post_thumbnail($linkvalue, 'thumbnail') . '</a>';
echo '<p class="year"><strong>Age</strong>: ' . $year . ' years old</p>';
echo '<p class="condition"><strong>Conditions</strong>:' . $medical . '</p>';       
echo '<p class="readmore"><a href="' . get_permalink($linkvalue) . '">read more</a></p>';
echo '</div>';
}; ?>

<?php endwhile; wp_reset_query(); ?>

If there is a cleaner/better way to write this, I would love to see it. Thanks guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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