0

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

<?php    

$posts = get_posts(array(


    'tax_query' => array(
            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
<?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.

5
  • 1
    Are you looking for the break keyword? Sep 24, 2021 at 21:10
  • Solved it, anyway, thanks for your effort, Ivan!
    – robert0
    Sep 24, 2021 at 21:37
  • orderby => 'rand' is generally a bad idea, for what it's worth.
    – vancoder
    Sep 24, 2021 at 22:05
  • I know, but that's the only solution that is actually doing what I want right now. But, I know it does take a lot of processes and slows down when more posts will be published. Will do the job for now, will think of the other randomizer solution later.
    – robert0
    Sep 24, 2021 at 22:08
  • 1
    If you only want 1 post, why are you asking for 999999999999999999? This query is extremely slow/expensive
    – Tom J Nowell
    Sep 24, 2021 at 22:30

1 Answer 1

0

Solved the problem by inserting this:

<?php if (in_array($counter, array(0)) ) :?>
// show content
<?php $counter++; endif;?>

The above basically displays only 1 post that is first in the array.

In case anyone needs to solve a problem like this.

2
  • I could see why you used a custom counter (in the if ($advertiser_userfunds > 0) block), but then why not just do <?php if ( 0 === $counter ) :?> instead of <?php if (in_array($counter, array(0)) ) :?>? And btw, the question is actually a generic PHP question and should instead be asked on Stack Overflow. 🙂
    – Sally CJ
    Sep 25, 2021 at 3:53
  • 1
    Also, variables need to be defined before attempting to use them, so make sure you define $counter, e.g. $counter = 0; before the foreach( $posts as $post )
    – Sally CJ
    Sep 25, 2021 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.