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


Getting 2 random items from post object of 4

    • dzoniis

    • December 4, 2018 at 8:16 am

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

    <?php $post_objects = get_field('items');
    $arr = [];
    if( $post_objects ): ?>
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
    $arr = get_the_id();
     setup_postdata($post); ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Post Object Custom Field: <?php the_field('price'); ?></span>
        <?php endforeach; ?>
        <?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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.