Support

Account

Home Forums Backend Issues (wp-admin) Getting 2 random items from post object of 4

Unread

Getting 2 random items from post object of 4

  • Hello, i want to get 2 random items from post object

    <?php $post_objects = get_field('items');
    $arr = [];
    if( $post_objects ): ?>
        <ul>
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php
    $arr = get_the_id();
     setup_postdata($post); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Post Object Custom Field: <?php the_field('price'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;?>

    i’ve managed to get all ids of posts inside, i got about 4 right now, but i need to shuffle them and display 2 random ones.

Viewing 1 post (of 1 total)

The topic ‘Getting 2 random items from post object of 4’ is closed to new replies.