user3387758
user3387758

Reputation: 77

How to compare post id in wordpress

I got the post ids which was posted before 1 day by using the below code.

I have to highlight these post id heading in front page (index.php).

$today  = current_time('mysql', 1);
$aa = 1;

if($recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_date > '" . date('Y-m-d', strtotime("-$aa days")) . "' ORDER BY post_modified_gmt DESC")):
  foreach($recentposts as $post) {
     echo $post->ID;
  } 
endif; 

within the have_post() loop how can check the condition " $post->ID == get_the_ID() " ?

Upvotes: 0

Views: 1201

Answers (2)

Daniel K.
Daniel K.

Reputation: 1199

There is a WordPress Reference for the WP-Functions. This will help you to get many information from WP itself (without querying the database directly).

For example you can use the function get_post($id) to get the post data by its ID (if you don't already have the data array) or directly using the get_the_ID() function.

EDIT: get_the_ID() is already described above ;)

With get_post() it would be something like this:

foreach($recentposts as $post) {
 echo "ID is: ".$post->ID."<br><br>";
 $p = get_post($post->ID);
 print_r($p);
} 

In the result array you have values with the timestamp (created / updated) and you can easily compare if this values ist "today" eg. by using something like this.

if(date("ddmmyy",time()-86400) == date("ddmmyy",$p['created'])){}

Upvotes: 1

bloomingsmilez
bloomingsmilez

Reputation: 421

If I understood correctly you want to compare the post id within the loop with some values. If so, you can try the following code

<?php while ( $query->have_posts() ) {
                    $query->the_post(); ?>
                    <?php if (get_the_ID() >value) {
                        ...highlight...
                       }
             }?>

Hope it helps.

Upvotes: 0

Related Questions