• Resolved kirasiris

    (@kirasiris)


    Hello, so I’m trying to do a custom wp query to show the last post of a certain category(business) but somehow the date is the only value that I get wrong. Can you help me?

    
    $business = array(
    'category_name' => 'business',
    'numberposts' => 1,
    'order' => 'DESC',
    );
    
    <ul class="fashion_catgnav">
    <?php $recent_posts = wp_get_recent_posts( $business  );?>
    <?php foreach( $recent_posts as $recent ): ?>
    <li>
    <div class="catgimg2_container">
    <?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . get_the_post_thumbnail($recent["ID"]).'</a>' ?>
    </div>
    <h2 class="catg_titile"><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>' ?></h2>
    <div class="comments_box">
    <span class="meta_date"><?php echo get_the_date($recent["ID"]) ?></span>
    <span class="meta_comment"><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . comments_number( 'no responses', 'one response', '% responses' ) .'</a>' ?></span>
    <span class="meta_more"><?php echo '<a href="' . get_permalink($recent["ID"]) . '">Read more</a>' ?></span>
    </div>
    </li>
    <?php endforeach; ?>
    </ul>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Are you really getting your posts returned with the above array configuration?

    I believe the right way of constructing the array should be like this:

    $business = array(
        'taxonomy' => 'category', /* instead of 'category_name'. */
        'posts_per_page' => 3, /* instead of numberofposts */
        'order' => 'DESC',
    );
    
    $recent_posts => wp_get_recent_posts($business);

    @subrataemfluence taxonomy isn’t a valid argument for get_posts(), which wp_get_recent_posts() uses under the hood, and wouldn’t make sense without also providing a term. numberposts is also a valid argument. Besides, as they say in their question, they are getting results, it’s just the date that’s not working.

    @kirasiris As you can see in the documentation for get_the_date() the first argument is for the date format, not the post for which to get the date. You can use '' to use the default date format for the site:

    <?php echo get_the_date( '', $recent["ID"]) ?>

    @jakept, Thanks so much! Howver, when I am trying @kirasisris’s code, I don’t get any result but using taxonomy I do. Would you please mind explaining why it is behaving different for me?

    @subrataemfluence taxonomy will just be ignored, so you’d be getting the 3 most recent posts, regardless of category. If category_name doesn’t work, it’d be because you don’t have the same categories.

    Thread Starter kirasiris

    (@kirasiris)

    Thank you so much Jacob, it works!!!; cannot believe it was just a small mistake way to easy to solve :v . Thanks!! . I will mark the post as solved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_the_date($recent[“ID”]) not working’ is closed to new replies.