• I have search filters which filter information from 1 of 2 tables based on filter choices. Those results are then put into an array. After that they are passed into $args. Here is what $args looks like:

    $args=array(
        'post_status'       => 'publish',
        'post_type'         => 'products',
        'tax_query'         => array(
            array(
                'taxonomy'  => 'product_categories',
                'terms'     => explode(',', $groups_separated),
                'field'     => 'slug',
            )
        ),
        'posts_per_page'	=> 30
    );

    My query_posts call with $args in my loop:
    $keep = query_posts($args);

    The order that $groups_separated is in when it goes into $args is the order that I would like the information to be displayed in. However when the loop is ran, the results appear to be displaying by ID example: WP_Post Object ( [ID] => 158.

    I have thought of two ways to possibly get my results to display in order, but not sure how to implement them. The first way would be to just have my loop display the results by order that they are passed into $args. The second, I think far more difficult, would be to join tables (my table from the search filter with wp_query) then add a sort. I am not sure how to go about either and there may be a better way, just looking for ideas or some guidance with either way I mentioned.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    This is a common problem without a clear solution because the specifics vary endlessly. I see 3 basic approaches. On low traffic/low content sites it probably doesn’t matter which approach is used.

    The easiest, and most inefficient, is to use PHP to make multiple small queries in the desired order to build the content.

    The second is more efficient, but trickier to code. You go ahead and get all the needed data in one big query, then use PHP’s usort() or some other algorithm to rearrange the data as needed before running the loop. usort() appears to be a bubble sort. It’s not too hard to code the sort callback, but bubble sorts are not very efficient. In addition, using PHP to do the sorting means the usual pagination used through WP_Query and it’s variants is not available.

    The most efficient, but most difficult to code, unless you’re a mySQL wizard is let mySQL do the sorting. All the data needed to do the sort of course needs to be in the database, and you need to determine the best way to join the various tables so the data can be sorted properly. In addition, the arguments accepted by query_posts() and other WP_Query variants are rarely sophisticated enough to manage anything but the most basic ordering.

    In order to send the proper mySQL query, you could simply use $wpdb->query() to send the actual query string. But doing so again means you cannot make use of the WP_Query pagination. Fortunately, you can use one or more of several filters to tweak the query generated by query_posts(), though it does involve more coding complexity.

    If your query results in pages of data, it’s best to go ahead and work with the filters so that pagination works seamlessly. If the results are relatively small and your site traffic load isn’t huge, go with whatever appeals to you.

Viewing 1 replies (of 1 total)
  • The topic ‘Remove sorting by ID’ is closed to new replies.