Avel
Avel

Reputation: 111

Wordpress display ordered meta key

This is my code:

<?php 
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->postmeta.meta_key = 'start_1'
OR $wpdb->postmeta.meta_key = 'start_2'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->postmeta.meta_value ASC
";
$schedulepost = $wpdb->get_results($querystr, OBJECT);
if ($schedulepost):
global $post;
foreach ($schedulepost as $post):
setup_postdata($post);
?>

<?php $start_1 = get_post_meta($post->ID, 'start_1', true); echo $start_1; ?>
<?php the_title(); ?>


<?php endforeach; endif; ?>

I have 2 meta keys of the same post to display, "start_1", "start_2". Using the code above, the result is 4 posts and ordered by meta value. The post with title movie is showing up twice because is the only one who has 2 meta keys, but the second meta key i cant make it work. I mean, the first meta key (start_1) value is Jul 04 07:00 PM, and the second meta key (start_2) value is Jul 04 10:30 PM.

Jul 04 07:00 PM - Movie

Jul 04 08:00 PM - Theater

Jul 04 09:00 PM - Sport

Jul 04 10:00 PM - Music

Jul 04 07:00 PM - Movie

How can i display 2 meta keys of the same post, of course showing up the post twice. Example, i make a post with the title, The Shawshank Redemption. Basically meta key is the date & time

(meta key: start_1) Jul 04 07:00 PM - The Shawshank Redemption

(meta key: start_2) Jul 05 10:30 PM - The Shawshank Redemption

Upvotes: 0

Views: 136

Answers (2)

Maulik patel
Maulik patel

Reputation: 2442

<?php
    $args = array (
                    'post_type'              =>'post',
                    'post_status'            =>'publish'
                );

    $the_query = new WP_Query( $args );
   while ($the_query->have_posts()): $the_query->the_post(); 
      $start_1 = get_post_meta( get_the_ID(), 'start_1', 'true');
      $start_2 = get_post_meta( get_the_ID(), 'start_2', 'true');
   endwhile;
?>

Upvotes: 1

Shrikant D
Shrikant D

Reputation: 837

Use query_posts or WP_Query instead of custom query. By this you can easily emit the duplicability in post id. For reference go to: Class Reference/WP Query

Upvotes: 1

Related Questions