wp_get_recent_posts() exclude specific post by id

2.2k Views Asked by At

I am trying to get the two most recent post but however i want to exclude a specific post by its id[365]. Can anyone help me with it? here's my code.

$args = array( 'numberposts' => '2' , 'post__not_in' => array( '365' ) );
$recent_posts = wp_get_recent_posts( $args );
 <?php foreach($recent_posts as $post):?>
    <a href="<?php echo $post['guid']?>"><p><?php echo $post['post_title'];?></p></a>
 <?php endforeach;?>
3

There are 3 best solutions below

0
WpTricks24 On BEST ANSWER

Just need to replace your code by below mentioned code, you will get your desired result :

<?php $my_args = array('post_type' => 'post' , 'numberposts' => '2' , 'exclude' => '365' );
$my_recent_posts = wp_get_recent_posts( $my_args );?>
 <?php foreach($my_recent_posts as $my_post):?>
    <a href="<?php echo $my_post['guid']?>"><p><?php echo $my_post['post_title'];?></p></a>
 <?php endforeach;?>
0
rnevius On

You should read the docs for that function again. You have an exclude parameter available:

$args = array( 'numberposts' => '2' , 'exclude' => 365 );
0
Noman On

wp_get_recent_posts Does not support post__not_in argument you will need to use exclude

Reference link for wp_get_recent_posts

<?php
$args = array( 'numberposts' => '2' , 'exclude' => '365' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post){ 
?>
     <a href="<?php echo $post['guid']; ?>">
        <p><?php echo $post['post_title']; ?></p>
     </a>
 <?php } ?>