I've a problem with my query_posts() call.

Here is my code:

$results = $wpdb->get_results("SELECT post_id FROM " . $wpdb->postmeta . " WHERE meta_key = 'mashsb_shares' ORDER BY CAST(meta_value AS SIGNED) DESC LIMIT 0, 20");

$top_ideas = array();

foreach($results as $result) {
    foreach($result as $key => $value) {
        array_push($top_ideas, $value);

query_posts( array( 'post__in' => $top_ideas, 'orderby' => 'post__in' ) );

My problem is that posts doesn't appear in the order of my post__in array ($top_ideas).

Here is the result

My print_r() shows the right array, so my $results's query works good. The problem is in my query_posts() call, but I can't find what problem is... I just understood that the query_posts() method is never called. If I take it out, nothing change.

Thanks for your help!


You may need to add this to your query_posts() call 'order' => 'ASC' so it should look like this:

query_posts( array(
    'post__in' => $top_ideas,
    'orderby' => 'post__in',
    'order' => 'ASC'
) );

Hope this helps!

  • Nothing changed. My query_posts() doesn't seem call... – Marcus Jul 22 '16 at 13:16
  • Just to make sure, are you running your code through the while loop? '<?php query_posts( array( 'post__in' => $top_ideas, 'orderby' => 'post__in', 'order' => 'ASC' ) ); while ( have_posts() ) : the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; ?>' – JasonC Jul 22 '16 at 13:20
  • Your code displays ids in the wrong order, but it is better than my code: 37, 34, 42, 58. This is very strange... – Marcus Jul 22 '16 at 13:39
  • My loop is just after my query_posts(), like yours. – Marcus Jul 22 '16 at 13:41
  • You may want to try and use WP_Query instead for your query. Find documentation on this link here codex.wordpress.org/Function_Reference/WP_Query. This will help alleviate any issues with your query or any other queries encroaching on the data. – JasonC Aug 16 '16 at 13:52

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.