0

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

Avel
  • 111
  • 1
  • 2
  • 9
  • Is there any particular reason why you are using custom query ? – Nilambar Sharma Jul 03 '15 at 03:48
  • you have not echo second meta and not geting second post mets by `$start_2 = get_post_meta($post->ID, 'start_2', true);` in your code above – Vijay Lal Jul 03 '15 at 07:00
  • @Nilambar i dont want to have duplicate content because of SEO. Example, same post different time `mywebsite/first-post and - mywebsite/first-post-2` Tecnically a post is an event. And an event is going to be on air twice in the day with same content, only the time (meta key start_1 and meta key start_2) is changing. So i want to display the samte post twice, based on the meta key the post has. If the post has 2 meta keys (start_1 and start_2), than the post will be displayed twice – Avel Jul 03 '15 at 10:35
  • @VijayLal i know, but that's not the problem. If i echo second meta key than i will have 2 timetables on the same post. What im trying to do, is displaying first the first meta key `$start_1`, than on the second raw, the second meta key `start_2`. – Avel Jul 03 '15 at 10:41

2 Answers2

1

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

Shrikant D
  • 807
  • 1
  • 8
  • 14
1
<?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;
?>
Maulik patel
  • 2,546
  • 2
  • 20
  • 26