admin管理员组

文章数量:1278950

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.

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.

Share Improve this question edited Sep 24, 2021 at 20:34 robert0 asked Sep 24, 2021 at 20:24 robert0robert0 2032 silver badges11 bronze badges 5
  • 1 Are you looking for the break keyword? – Ivan Shatsky Commented Sep 24, 2021 at 21:10
  • Solved it, anyway, thanks for your effort, Ivan! – robert0 Commented Sep 24, 2021 at 21:37
  • orderby => 'rand' is generally a bad idea, for what it's worth. – vancoder Commented 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 Commented 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 Commented Sep 24, 2021 at 22:30
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpDisplay just one post from a loop of 5 posts