php – How to show 1 random post result without using an array?


Ok so basically I have this kind of situation.

My first step is to filter out the posts using an array, let’s say using this:

$posts = get_posts(array(
    'post_type'         => 'post',
    'posts_per_page'    => 99999999,

    'meta_query' => array(
                'key'     => 'status',
                'value'   => 'active',

    'orderby'               => 'rand'

So, let’ say after the above code I’m left with a 20 random posts.

On the second step I use the PHP filter (which is not possible in the above array):

$postid = get_the_ID(); 
$a1=get_post_meta( $postid, 'a1' , true );
$a2=get_post_meta( $postid, 'a2' , true );
if ($a1 < $a2): ?>

    <?php the_title(); ?>  

<?php endif; ?>

The above filter’s out the results, and after that I’m now left with, let’s say, 5 post results.

My question is, is there a php code that will choose just 1 random post from that list?

I don’t need 5, I just need 1.

Hope I’m clear.

Desperately need help here.

robert0 1 month 2021-09-16T05:49:00-05:00 0 Answers 0 views 0

Leave an answer