WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_post_time of a specific post (4 posts)

  1. jitpal
    Member
    Posted 4 years ago #

    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.

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //display date for post id 502
    $one=get_post(intval(502));
    if ($one) {
    echo mysql2date("F j, Y", $one->post_date);
    }
    ?>
  3. jitpal
    Member
    Posted 4 years ago #

    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.

  4. jitpal
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags