• Resolved jitpal

    (@jitpal)


    I have a quick question. I wanted to know how I can get the post time of a specific post, by post id preferably. Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php
    //display date for post id 502
    $one=get_post(intval(502));
    if ($one) {
    echo mysql2date("F j, Y", $one->post_date);
    }
    ?>
    Thread Starter jitpal

    (@jitpal)

    Hmm, I’m not sure that code will get me to where I need to go although I could be interpreting it incorrectly. Let me give some context. I’m trying to put together a list of posts on the single post page that match a specific custom field value. Then I want to take that list and attach an icon indicating that it’s a new post. I’ll attach the important parts of code I’m using.

    <?php
    	global $wpdb;
    
    	$findposts = $wpdb->get_results(
    		"SELECT *
    		FROM $wpdb->posts
    		LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
    		WHERE $wpdb->postmeta.meta_key = 'Serial'
    		AND $wpdb->postmeta.meta_value = '$serial_value'
    		AND $wpdb->posts.post_status = 'publish'
    		AND $wpdb->posts.post_type = 'post'
    		AND $wpdb->postmeta.post_id != $id
    		ORDER BY $wpdb->posts.post_date ASC"
    	);
    ?>
    
    	<ul>
    
    <?php $limit = 6; ?>
    <?php foreach ($findposts as $findpost): ?>
    	<?php $counter_serp++; ?>
    	<?php $fp_id = $findpost->ID;
    		$fp_title = $findpost->post_title;
    		$fp_url = $findpost->guid;
                    $fp_date = $findpost->post_date;
    	?>
    	<?php $mylimit=60 * 86400; $post_age = date('U') - $fp_date;?>
    
    		<li class="related">
    			<?php if ($post_age <= $mylimit) { ?><span class="info_icon new">New</span> <?php } ?>
    			<a href="<?php echo $fp_url; ?>" title="Permalink to <?php echo $fp_title; ?>"><?php echo $fp_title; ?></a>
    		</li>
    
    	<?php if($counter_serp >= $limit) break; ?>
    <?php endforeach; ?>
    	</ul>

    So basically I need to either get the post date by post ID and convert that to seconds so I can compare or convert the post_date string into seconds. Any ideas? Thanks.

    Thread Starter jitpal

    (@jitpal)

    Nevermind, I think I got it. Using the bit of code you showed before, I changed it to output the date in Unix format. Thanks for the help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_post_time of a specific post’ is closed to new replies.