php – Display just one post from a loop of 5 posts?


Let’s say I have this Query to which I fetch all my WP posts and then use PHP to filter out even further:


$posts = get_posts(array(

    'tax_query' => array(
                'taxonomy' => 'type',
                'field'    => 'slug',
                'terms'    => array( 'campaign' ),
    'post_type'         => 'post',
    'posts_per_page'    => 999999999999999999,
    'orderby'               => 'rand',

if( $posts): ?>
            <?php foreach( $posts as $post ): setup_postdata( $post ) ?>

// Filter out users with funds with PHP
$post_id = get_the_ID();
$advertiser_id = get_post_field( 'post_author', $post_id );
$advertiser_userfunds=get_user_meta( $advertiser_id, 'userfunds' , true );
if ($advertiser_userfunds > 0):?>

//5 posts left in the loop after filtering out users with funds

<?php endif;?>      

<?php endforeach; ?>
 <?php wp_reset_postdata(); ?>

<?php endif; ?>         

So let’s say I’m left with a loop of 5 posts now.

My question is, is there any way to show just 1 post?

It can be in a random order, or simply showing first or last post from the loop?

I know there’s a php code if in_array(). Maybe I could implement this somehow?

Desperately need help.

robert0 11 months 2021-09-24T15:24:26-05:00 0 Answers 0 views 0

Leave an answer