0

Right now my site shows 3 random posts at the end of my single page articles. Here is the code I use and it works (with comments):

                    <?php 
                    $rand_posts = get_posts('numberposts=3&orderby=rand&post_type=any&cat=-15');    
                        /*  
                        Alternative Expression: (include all post types, exclude catefory 'site' in the following code we define the post types explicitly instead of 'any'")
                        $args = array(
                        'orderby'       => 'rand',
                        'posts_per_page'   => 3,
                        'category'      => -1,
                        'post_type'     => array( 'post', 'page', 'oldp' )
                        );
                        $rand_posts = get_posts( $args );
                        */

                        foreach( $rand_posts as $post ) : ?>

I would like the random posts to be my best posts, that is to be from a pool of posts I will choose or somehow identify. How can this be done.

I also have a code for random quotes that is quite simple and I wonder if I could use something as simple as that to specify the random posts pool:

$quotes = array(
    'Quote1',
    'Quote2',
    'Quote3',
); 
return $quotes[rand(0, count($quotes)-1)];
2

Quick & simple: You could simply use post__in in your query with the post IDs of those in your pool, though it might be less easy to maintain when you add and remove posts.

Or you could use a meta field and change your query to include it, see the WP_Query documentation. Then either set that field manually in the post editor or use a Plugin (Advanced Custom Fields, Custom Field Suite etc pp) to generate a fancy looking checkbox.

To use the custom field version, change your get_posts() call from

$rand_posts = get_posts('numberposts=3&orderby=rand&post_type=any&cat=-15');    

to

$rand_posts = get_posts('numberposts=3&orderby=rand&post_type=any&cat=-15&meta_key=toppost&meta_value=1');    

and you should be up and running, assuming the meta field you're adding is called toppost and you've set its value to 1 for the top posts you want in your pool.

| improve this answer | |
  • This seems to be what I'm looking for. Can you please post the specific code, to see what it would look like? Thanks – IXN Oct 16 '17 at 14:17
  • 1
    That'd be as simple as adding meta_key=toppost&meta_value=1 to your get_posts() call and adding a the meta value to your posts. – janh Oct 16 '17 at 14:30
  • Please edit your answer and add the complete code. Right now it is too fragmented and I can't choose it as best answer. – IXN Oct 16 '17 at 14:34
  • 1
    It's copy & paste-ready now. – janh Oct 16 '17 at 14:41
  • 1
    @IXN, it is actually very basic idea that just follows the answer.... get all the posts without randomizing them, and then use php rand function php.net/manual/en/function.rand.php to decide which of them you want to display. It assumes that the number of possible posts is not very big. If the number is big there is a trade off of DB performance vs PHP performance which is harder to quantify. – Mark Kaplun Oct 16 '17 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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