• Bazan

    (@bazan)


    I want to get 3 random posts from my custom post type posts.

    My post type name = ‘portfolio’

    The code display last posts but not in random order, always the 3 last pages :/ What I have to do :/

    I have this code:

    <?php
    	remove_all_filters('posts_orderby');
    
    	$argss=array(
    		'post_type'=>'portfolio',
    		'orderby'=>'rand',
    		'posts_per_page'=>'3',
    		'post__not_in'=>array($post->ID)
    		);
    
    	$queryObjects = new WP_Query($argss);
    	// The Loop!
    	if ($queryObjects->have_posts()) {
    	   ?>
    	   <aside class="random-pages-container clearfix">
    	   <h2 class="random-header"><?php _e('Inne realizacje'); ?></h2>
    	   <?php
    	    while ($queryObjects->have_posts()) {
            $queryObjects->the_post();
            ?>
    
            <article class="random-page">
    			<a href="<?php the_permalink();?>" title="<?php the_title();?>">
    			<?php
    			if ( has_post_thumbnail() ) {
    				the_post_thumbnail();
    			}
    			else {
    				echo '<img src="' . get_bloginfo( 'template_directory' ) . '/images/empty-th.jpg" alt="<?php the_title();?>" width="235"/>';
    			}
    			?>
    
    			<h3 class="random-title"><?php the_title();?></h3>
    			</a>
    		</article>
       	 <?php
    		} //endwhile
    		?>
    	</aside>
    	<?php
    	} //endif
    	wp_reset_postdata();
    
    	?>

    I have also tried query_posts($args) but dont work.

  • The topic ‘wp_query orderby rand not working’ is closed to new replies.