get posts - get_post random and order by not working

I have this code that returns list of post title as links, but when I add the 'orderby' and 'order'

I have this code that returns list of post title as links, but when I add the 'orderby' and 'order' parameters - it returns results but 'orderby, order, rand' do not work, can anyone tell me what I'm doing wrong? Thanks!

<ul>
   <?php $post; $cat_posts = get_posts(array('numberposts' => 10, 'orderby' => 'rand', 'order' => ASC, 'category' => $disciplineCatID));
   foreach($cat_posts as $post) : ?>
   <?php $postTitle = get_the_title(); if($title != $postTitle) :?>
   <li><a href="<?php the_permalink(); ?>">&rsaquo;&rsaquo; <?php the_title(); ?></a></li>
   <?php endif ;?>
   <?php endforeach; ?>
</ul>

I have this code that returns list of post title as links, but when I add the 'orderby' and 'order' parameters - it returns results but 'orderby, order, rand' do not work, can anyone tell me what I'm doing wrong? Thanks!

<ul>
   <?php $post; $cat_posts = get_posts(array('numberposts' => 10, 'orderby' => 'rand', 'order' => ASC, 'category' => $disciplineCatID));
   foreach($cat_posts as $post) : ?>
   <?php $postTitle = get_the_title(); if($title != $postTitle) :?>
   <li><a href="<?php the_permalink(); ?>">&rsaquo;&rsaquo; <?php the_title(); ?></a></li>
   <?php endif ;?>
   <?php endforeach; ?>
</ul>
Share Improve this question edited Mar 23, 2011 at 13:42 asked Mar 23, 2011 at 7:46 user3907user3907 3
  • 1 Missing single quotes on the ASC.. just a typo or possibly related? – t31os Commented Mar 23, 2011 at 10:49
  • Could you be more specific on what's not working (e.g. does your code not return any results or ...)? – Manzabar Commented Mar 23, 2011 at 12:55
  • @t31los, tried the quotes - thanks but still no luck. @Manzabar - thank you - I've updated the information. Results are returned 'order, orderby, rand' do not take effect. Thanks you. – user3907 Commented Mar 23, 2011 at 13:43
Add a comment  | 

7 Answers 7

Reset to default 22

Yes, this is the correct syntax:

$args = array(
    'orderby' => 'rand',
    'order'    => 'ASC'
);
query_posts( $args );

However plugins can keep this from working properly. Try disabling ALL plugins and see if that helps. Two known plugins which keep orderby=rand from working are Post Type Order and WP_Sticky

Also, if you have Post Types order installed make sure you visit the Admin page and check the settings. You can use this plugin and keep it from automatically re-ordering posts:

http://img829.imageshack.us/img829/2616/pictureot.png

And then you can use the code for Post Types Order to specifically order those posts in places where you need them to be ordered via the custom/menu-order. Here is the example code for that plugin:

The following PHP code will still return the post in the set-up Order:

$args = array(
'post_type' => 'feature'
);

$my_query = new WP_Query($args);
while ($my_query->have_posts())
{
$my_query->the_post();
(..your code..)          
}

Or:

$posts = get_posts($args);
foreach ($posts as $post)
{
(..your code..)     
}

If the Auto Sort is uncheck you will need to use the "orderby" and "order" parameters:

$args = array(
'post_type' => 'feature',
'orderby'   => 'menu_order',
'order'     => 'ASC'
);

If you're hosted on WP Engine, you need to manually enable random ordering in your settings or else 'orderby' => 'rand' won't work.

https://wpexplorer-themes/total/docs/random-order-wpengine/

Why don't you try to use query_posts instead?

Something like:

$args = array(
    'orderby' => 'rand',
    'order'    => 'ASC'
);
query_posts( $args );

try this code

<?php 
   remove_all_filters('posts_orderby');
   query_posts('orderby=rand'); 
?>

got the answer from here

http://www.reinaris.nl/wp/wordpress-random-post-order-not-working-orderbyrand/

You probably forgot to setup_postdata inside your foreach loop to get template tags to work.

I ran into the same problem, and luckily found this thread. I tried a different fix, probably dirtier than the others, but maybe useful in some cases.

Basically, I shuffled the posts before outputting them:

<?php 
    $selectedPosts = get_posts($args);
    shuffle($selectedPosts);

    foreach ($selectedPosts as $selectedPost) : 
    setup_postdata( $selectedPost );
?>

<!-- post elements here -->

<?php 
    endforeach;
    wp_reset_postdata();
?>

If you have wp-engine in your WordPress site check the ALLOW ORDER BY RAND() option. See an article here: https://wpengine.co.uk/support/about-order-by-rand/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744729793a4590399.html

相关推荐

  • get posts - get_post random and order by not working

    I have this code that returns list of post title as links, but when I add the 'orderby' and 'order'

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信