WordPress.org

Forums

Rand order inst work ? (7 posts)

  1. rakisuy
    Member
    Posted 2 years ago #

    Hi, I'm trying of show a relationships posts, I use this code:

    <?php
    						$categorias = array();
    						$categories = get_the_category();
    						foreach($categories as $categoria){
    
                                array_push($categorias, $categoria->cat_ID);
    						}
    						?>
                            <div id="similares">
                            	<h3 class="entry-title" style="margin: 20px 0 10px 0; width: 540px;">Relacionados</h3>
    
    							<?php
    							$args = array('showposts' => 3, 'orderby' => 'rand', 'category__and' => $categorias);
                                print_r($args);
    							$relac = new WP_Query($args);
                                if($relac->have_posts()):
                                    while($relac->have_posts()): $relac->the_post(); ?>
    									<div class="product-wrap">
                                        <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo crop(get_the_title(),45,false); ?></a></h3>
                                        <div class="image"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php $imagenurlthum = get_thumbnail_src($post->ID); if(!empty($imagenurlthum)): ?><img height="115" src="<?php bloginfo('template_directory') ?>/timthumb.php?src=<?php echo $imagenurlthum; ?>&w=153" title="<?php the_title() ?>" alt="<?php the_title() ?>" /><?php else: ?><img height="115" src="<?php bloginfo('template_directory') ?>/timthumb.php?src=<?php bloginfo('template_directory') ?>/images/logo_login.gif&w=153" title="<?php the_title() ?>" alt="<?php the_title() ?>" style="margin: 25px 0 0" /><?php endif; ?></a></div>
                                        <div class="excerpt"><?php echo crop(strip_tags(get_the_excerpt()),80); ?></div>
                                        <div class="price"><?php echo get_post_meta($post->ID,'Precio',true) ?></div>
                                    </div>
                                    <?php
                                    endwhile;
                                endif;
                                ?>

    If I use category parameters, will display a correct post, but never different posts, without variation !

  2. bcworkz
    Member
    Posted 2 years ago #

    To get the mySQL random function, you need to include the parenthesis: 'RAND()' otherwise you are telling SQL to order the results by the column named 'rand'.

    I have heard ORDERBY RAND() is very inefficient and you're better off getting all possible results and randomly picking 3 posts by index number, though by limiting the query to 3 posts perhaps that is not so bad. It'd be interesting to time each approach.

  3. rakisuy
    Member
    Posted 2 years ago #

    I'm using the class WP_Query, and his doc for do it !
    How do you say that I can use random function ?

  4. bcworkz
    Member
    Posted 2 years ago #

    Oops! You are right, I misunderstood how orderby works in the WP_Query context, my apologies. I cannot explain why the results repeat, your usage appears correct.

    Since it's inefficient, and does not work anyway, you could query for all posts in the categories you want, then instead of a while loop, do something like

    global $post;
    for ($i = 1; $i <= 3; $i++) {
      $post = $relac[rand(0,count($relac)-1)];
      // do the usual title-permalink-content-etcetera stuff
    }

    There is a chance of duplicate posts this way, you could add code to push used post ids into an array and retry random posts if the current id is in the array already.

  5. webmasterobservatoriobioetica
    Member
    Posted 2 years ago #

    Hi, I have a similar problem.

    I'm making a page, and I need to order some posts belonging to two specific categories, for this, I looked around, and I have to use the category__and. So much perfect.

    The problem is that if I use the post category__and not sorted from newest to oldest, it is sorted by date of creation, the first post created the first and last end.

    code used:

    CATEGORY__IN = WORK:

    if( trim( $pagination ) == 'true' ) {
    	$paged = mysite_get_page_query();
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__in' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'posts_per_page' => $showposts,
    		'paged' => $paged,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    
    } else {
    
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__in' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'showposts' => $showposts,
    		'nopaging' => 0,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    }

    WITH CATEGORY__AND = NOT WORK

    if( trim( $pagination ) == 'true' ) {
    	$paged = mysite_get_page_query();
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__and' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'posts_per_page' => $showposts,
    		'paged' => $paged,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    
    } else {
    
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__and' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'showposts' => $showposts,
    		'nopaging' => 0,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    }

    If I use category__in, all right, if I use category__and, not working :(

    Can you help me?? Thanks

  6. bcworkz
    Member
    Posted 2 years ago #

    You are saying the order parameter acts as if it were 'ASC' only when you use the 'category__and' argument, though you clearly are requesting the order to be 'DESC'?

    This is quite strange. The category__and code does nothing with order parameters at all in source code. Perhaps a plugin or your theme is filtering queries for another purpose and your query is getting accidentally caught in that filter? Try deactivating plugins and even switching themes, just to see if this odd behavior goes away.

    You can also examine the query var 'order' to ensure it is parsed correctly. Or try hooking (with a high numbered priority, execute last parameter) the 'posts_request' filter to see that the actual mySQL query is as you expect. If all this checks out, I'd have to say it's some SQL quirk rather than WP.

    If you are desperate to get something working, change the order parameter to ASC for compatibility, then use array_reverse() to get the correct order.

    BTW, in the future, we prefer you start your own topic when you have a question, even if similar to someone else's. It avoids confusion and your question is more likely to be seen by more people, increasing your chances of getting a good answer. Good luck tracking down the cause of this mystery.

  7. webmasterobservatoriobioetica
    Member
    Posted 2 years ago #

    Ok, sorry,

    I changed this in the wp-includes/query.php line 2283 (WordPress V3.5) and now it's work correctly... it's correct?

    Change this

    if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    $groupby = "{$wpdb->posts}.ID";
    }

    for this:

    if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    //$groupby = "{$wpdb->posts}.ID";
    }

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic