Add random custom posts to the wordpress loop

I have a problem Im trying to solve and not having much luck, Iv tried google/stackoverflow etc, but all I find in general ‘random post’ type answers

here’s the issue:

Read More

I am getting my posts using a pretty standard loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php $cat = get_the_category();
    if(strtolower($cat[0]->name) != 'hidden'){
  ?>

I then display all the post data etc, etc.

I have a custom post type, with an option in the back end to limit the number of these custom posts that can be shown on the homepage

get_option('max_amount')

What I want to do is mix some of the custom posts (custom post type) in with the posts from the loop, not exceeding the ‘max_amount’ BUT I dont want them next to each other, I need them mixed in.

Hope that all makes sense and someone can help or point me in the right direction

Thanks

Related posts

Leave a Reply

1 comment

  1. If you add the posts to and array you could then use shuffle($posts) to randomise the post. Then use $posts = array_slice($posts, 0, get_option('max_amount') to ensure you don’t exceed the max amount.