C
C
Creel2015-09-30 13:49:35
WordPress
Creel, 2015-09-30 13:49:35

Where does sorting come from in $wp_query or how can I disable it?

I'm trying to display "my" products, but for some reason they are sorted incorrectly

while($aa = mysql_fetch_array($a)){				
      array_push($p_id, $aa['post_id']);				
    }
    if(count($p_id) == 0){
      ***
    } else {
      $args['post__in'] = $p_id;
    }
    $args['post_type'] = 'product';
  }	
// в $wp_query->query сейчас находится только Array ( [post_type] => product ) 
  query_posts(array_merge($args,$wp_query->query));

in the $p_id array
Array
(
    [0] => 30788
    [1] => 30787
    [2] => 30786
    [3] => 36196
    [4] => 33569
    [5] => 33568
    [6] => 22032
    [7] => 22745
    [8] => 29516
    [9] => 26660
    [10] => 22033
    [11] => 33396
    [12] => 33397
    [13] => 35537
    [14] => 34520
    [15] => 34521
    [16] => 35805
    [17] => 35263
    [18] => 35536
    [19] => 34519
    [20] => 28829
    [21] => 28827
    [22] => 35599
    [23] => 28826
    [24] => 35595
    [25] => 35598
    [26] => 35535
    [27] => 34522
    [28] => 35400
    [29] => 34523
    [30] => 35398
    [31] => 35259
    [32] => 35260
    [33] => 19384
    [34] => 35262
    [35] => 36194
    [36] => 29942
    [37] => 35261
    [38] => 33316
    [39] => 30567
    [40] => 28887
    [41] => 29501
    [42] => 23869
    [43] => 28880
    [44] => 23205
)

But the output is:
36194
36196
30788
33316
28880
34519
...
$args['orderby'] = 'none'; does not help, but rather sorts in order
overwriting
$wp_query->query_vars->orderby = "";
$wp_query->query_vars->order = "";

gives nothing
even stupidly query_posts($args); doesn't change anything
checked, maybe it doesn't pick up something but $args['nopaging'] = 1; works great

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
Creel, 2015-10-01
@80689248440

This is genius! $args['orderby'] = 'post__in';

M
MetaDone, 2015-09-30
@MetaDone

$args['post__in'] = implode(",",$p_id);
and in $args you need to enter
and in general it’s better to do everything through WP_Query, and not through query_posts
And it’s not very clear why pull mysql directly, this is a hell of a crutch, use wpdb to select such products

M
Mr Crabbz, 2015-09-30
@Punkie

Try to construct your wp_query through this service: https://generatewp.com/wp_query/ - you can also set orderby = "none" there.
Perhaps you made a mistake somewhere in a trifle or a sign and simply do not see an obvious mistake. It's hard to tell from the code above. The purpose of such a conclusion is unclear. After all, in some order, the posts should be displayed all the same, right?

I
Igor Vorotnev, 2015-10-01
@HeadOnFire

Creel : God forbid using such code, you have sql-injection there! I'm silent about the dangerous output. This time. Two - a direct connection to the database and a low-level sql query inside the WP environment - complete cretinism. Three - read about hooks. WooCommerce (and WP) has a lot of useful events and filters for such tasks.
Take a step back, get rid of this leaky shit code. Set a clear task. Then you can do everything right.
PS: According to the link you provided, the code is normal, but you got a nightmare.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question