Randomize Posts. Skip the first post in ascending order

I’d like to randomize a set of posts (custom post type), but ignore the first post all together.

Here’s the code I thought would work:

Read More
$featured_content_query = new WP_Query( array(
  'post_type'       => 'feature',
  'offset'          => 1,
  'posts_per_page'  => 10,
  'order'           => 'ASC',
  'orderby'         => 'rand'
) );

Without the orderby the post I want skipped over is ignored, but as soon as I add the random order it adds it back into the mix.

Does anyone know what I’m doing wrong? I feel like I’m missing something really obvious here.

Thnx!

Related posts

2 comments

  1. I believe there are different ways of achieving this. Simplest way I could imagine is as below:

    $featured_content_query = new WP_Query( array(
      'post_type'       => 'feature',
      'offset'          => 1,
      'posts_per_page'  => 10,
      'order'           => 'ASC'
     ) );
    
    shuffle( $featured_content_query );    //PHP function to randomise posts array
    

    You can then continue to use $featured_content_query as usual!

  2. I was looking for another answer and stumbled on this, thought i could help.

    If you looking to exclude the most recent post(s) you may be able to use wp_get_recent_posts() to retrieve the ID(s), and then use 'post__not_in' => array(#,#) in your WP_Query to exclude those resent posts.

    Tested this a little and found this to work for getting ONE post to be excluded. I think you could parse the results differently to get more than just on ID if you so desired.

    $recent = wp_get_recent_posts(array('post_type'=>$type,'numberposts'=>'1'));
    $recentID[] = $recent[0]['ID']; // this creates an array of one
    
    $args = array(
         'post_type'        => $type
        ,'post_staus'       => 'publish'
        ,'post__not_in'     => $recentID // array()
    

Comments are closed.