WordPress Mike
WordPress Mike

Reputation: 458

Custom Post Type Loop Only Displays First Post Meta Data

I am querying a custom post type in an archive template. Each post has meta values attached. The loop does list each post but the meta value on each post is pulling from the first post. So, I have a list of posts and all the meta values are the same, the data saved on the first post. The data is saved properly in the database. Here is my loop:

$current_time = current_time('mysql'); 
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $current_time );
$current_timestamp = $today_year . $today_month . $today_day . $hour . $minute;


$args = array( 'post_type' => 'event',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'posts_per_page' => 20,
'meta_key' => '_start_eventtimestamp',
'meta_query' => array(
    array(
        'key' => '_start_eventtimestamp',
        'value' => $current_timestamp,
        'compare' => '>'
    )
)
 );
// Gets the event start month from the meta field
$month = get_post_meta( $post->ID, '_start_month', true );
// Converts the month number to the month name
//$month = $wp_locale->get_month_abbrev( $wp_locale->get_month( $month ) );
// Gets the event start day
$day = get_post_meta( $post->ID, '_start_day', true );
// Gets the event start year
$year = get_post_meta( $post->ID, '_start_year', true );
$numdays = get_post_meta( $post->ID, '_tour_numdays', true );
$price = get_post_meta( $post->ID, '_tour_price', true );

query_posts( $args );

    while (have_posts() ) : the_post();
    echo '<ul>';
        echo '<li>' . $month . '/' . $day .'</li>';
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        echo '<li>' . $numdays . '</li>';
        echo '<li>' . $price . '</li>';
        echo '<li><a href="#">Join this Trip</a></li>';
    echo '</ul>';
    endwhile;

I cannot find a solution. I have tried changing the query from WP_Query to query_posts but still no luck.

Upvotes: 1

Views: 1891

Answers (1)

rlatief
rlatief

Reputation: 723

Here's your logic:

Get Post Metas ($price, $month, etc.)

Query Post (retuned an array of posts)

Begin looping the array of posts

Echo the results, including Post Metas ($price etc) which is NOT the metas of each post

End loop

You need to fetch the metas of each post by using get_post_meta() inside the loop.

You're currently doing:

$month = get_post_meta( $post->ID, '_start_month', true );

... $post->ID is NOT the ID of each post, only the first one.

Here's a sample of what the while() should look like (I'd better not provide an exact sample as I might missed something that could add to the confusion):

while (have_posts() ) : the_post();

$month = get_post_meta( $post->ID, '_start_month', true ); // here it is
// $day = ... continue here

echo '<ul>';
    echo '<li>' . $month . '/' . $day .'</li>';
    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    echo '<li>' . $numdays . '</li>';
    echo '<li>' . $price . '</li>';
    echo '<li><a href="#">Join this Trip</a></li>';
echo '</ul>';
endwhile;

Upvotes: 2

Related Questions