Last post ordered by publish date using WP_Query on front page

I’m joining together few websites into one and I created appropriate posts and changed their post_date (publish date) to the original time of post. However (even if I list more than 1 post) the result, I get, is the last post according to quite recent ‘post_modified’ (last update of the post).

// EDIT – more detailed explanation of the problem

Read More

My original post on the old website is from, let say, Jan 1 2001 (original date). Next, I create a post on the new website on Sep 22 2013 (today) and manually set the publish date in the Post Edit area to the original date. I can see in the $wpdb post_date=2001-01-01 and post_modified=2013-09-22. The last post shortcode return the post with the latest post-modified date instead of the latest post_date.

// EDIT – end

I’m running a custom WP_Query within a plugin’s slide, using a short tag to place it there. The problem is, that it takes the last modified post instead of last published post.

My code for the shorttag:

//[latestpost]
function latestpost_func( $atts ){
    global $post;
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $query = new WP_Query( array( 'paged' => $paged ) );
    $latestpost_arg = array(
                          'post_type' => 'post',
                          'posts_per_page' => 1,
                          'orderby' => 'date',
                          'order' => 'DESC',
                          'paged' => $paged,
                          'ignore_sticky_posts' => 1,
                          'category__in' => array( 127, 253 )
            );
    $latestpost_query = null;
    wp_reset_query();
    $latestpost_query = new WP_Query($latestpost_arg);


     if ( $latestpost_query -> have_posts() ) :
      while ($latestpost_query -> have_posts()) :
            $latestpost_query -> the_post();
            if (has_post_thumbnail()) {
                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
                $latestpost_image_div = '<div class="slide-post-image"><img src="' . $large_image_url[0] . '" /></div>';
            }

            $latestpost_details_div = '<div class="slide-post-details-div"><h2 class="latest-post-title">' . get_the_title() . '</h2><p class="latest-post-excerpt">' . get_the_excerpt() . '</p></div>';

            $latestpost_output = $latestpost_image_div . $latestpost_details_div;
      endwhile;
     else :
        $latestpost_output = 'Sorry, no posts matched your criteria.';
     endif;

  return $latestpost_output;
 }

 add_shortcode( 'latestpost', 'latestpost_func' );

Related posts

2 comments

  1. The following 3 parameters will give you the posts in Ascending order from the date it was published (i.e The older posts will be shown first)

    ‘post_status’ => ‘publish’, ‘orderby’ => ‘publish_date’, ‘order’ => ‘ASC’

    When you change the order to DESC you will get the posts in Descending order from the date it was published (i.e The latest posts will be shown first)

    ‘post_status’ => ‘publish’, ‘orderby’ => ‘publish_date’, ‘order’ => ‘DESC’

    <?php
    $postsPerPage = 10;
    $page = 1;
    ?>
    <?php
    $query = new WP_Query(array(
        'cat' => 4,
        'post_status' => 'publish',
        'orderby' => 'publish_date',
        'order' => 'ASC',
        'paged' => $page,
        'posts_per_page' => $postsPerPage));
    ?>
    
  2. I’ve found what the problem was. I have a plugin, Custom Post Order running to be able to sort out my CPTs. This plugin was overriding the post order. After switching it off for posts in the CPO’s settings everything behaves as it’s supposed to.

Comments are closed.