Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type randomized posts WordPress

Hello I have the custom post type = 'portfolio'
I would like to have these randomly sorted, what code would I need to do that in what PHP file?

Answers (4)

2012-09-04

Abdelhadi Touil answers:

Hi.
you can try this:
<?php
$loop = new WP_Query(array(
'post_type' => 'portfolio',
'orderby'=>'rand'
));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile; ?>

the key code is 'orderby'=>'rand'.
You can also use query_posts:
[[LINK href="http://codex.wordpress.org/Function_Reference/query_posts"]]http://codex.wordpress.org/Function_Reference/query_posts[[/LINK]]
Good luck.

2012-09-04

webGP answers:

Hi!

use this query to get random portfolio posts:


$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'orderby' => 'rand'
);

query_posts($args);


liv comments:

Where do I place this code? Under the PHP file that the page is accessing correct? Or the functions.php?


webGP comments:

Hello!

You should put this code before the loop which displays your portfolio posts.

2012-09-05

Arnav Joy answers:

try this


<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
<?php query_posts( array( 'post_type' => 'portfolio','orderby' => 'rand' , 'paged' => $paged ));?>
<?php if(have_posts()): ?>
<?php while ( have_posts() ) : the_post(); ?>

<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

<?php the_content(); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>

2012-09-06

Kristian Primdal answers:

Well it is almost answered, but nobody told you which file to edit. The easiest world be to make a new file called: archive-portfolio.php which you place in /wp-content/themes/{theme-used}/

Then you just insert what all of them have already said, but rememeber at least also a header and footer, I don't know if you use Sidebar, so maybe look in your archive.php, to get an idea of what is missing if something is missing:



<?php get_header(); ?>

<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>

<?php query_posts( array( 'post_type' => 'portfolio','orderby' => 'rand' , 'paged' => $paged ));?>

<?php if(have_posts()): ?>

<?php while ( have_posts() ) : the_post(); ?>



<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>



<?php the_content(); ?>



<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query();?>

<?php get_footer();?>