Support

Account

Home Forums General Issues Randomize multiply Post Objects

Helping

Randomize multiply Post Objects

    • hubbe

    • July 12, 2019 at 6:35 pm

    Hello im working on a slider on my site. I am using post object field to get the content for the slider. everything is working fine except that im trying randomize the order of the posts that is in the field. the setup is pretty much this https://www.advancedcustomfields.com/resources/post-object/ and im trying to use filters to change the order.

            function my_post_object_query( $args, $field, $post_id )
            {
                // modify the order
                $args['orderby'] = 'rand';
            
            
                return $args;
            }
            
            // filter for a specific field based on it's name
            add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 1);
  • The filter that you are trying to use only works in the admin when selecting posts, it does nothing on the front end of the site. On the front end ACF always orders the posts in the order select. If you want to have a different order then you need to do your own query on the posts. Get the field without formatting and then use that in the post__in argument for WP_Query().

    
    // get unformatted value
    // will return an array of post IDs
    $post__in = get_field('post_object_field', false, false);
    
Viewing 2 posts - 1 through 2 (of 2 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.