0

I trying create a code that will retrieve the first post from a category and echo the link.

But, the code always getting permalink of current post. Can anyone help me to fix it?

    <?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
        $array = new WP_Query(array(
            'category__in' => array($category),
            'post_per_page' => 1,
            'order' => 'asc',
            'orderby' => 'id'

    ));?>
    <?php the_permalink($post->ID); ?>
niznet
  • 107
  • 1
  • 11

1 Answers1

1
$args = array(
            'numberposts' => 1, 
             'orderby' => 'post_date',
            'order' => 'ASC', 
            'fields'          => 'ids',
            'category' => 8 //or use further logic to check in list
        );
     $post_cus = get_posts($args);
     $first_post_id = $post_cus[0];
     $post_url = get_the_permalink ($first_post_id);
Nabeel Perwaiz
  • 193
  • 1
  • 7