• I have an installation of wordpress 3.1.1 and this has been happening since v. 3.1 at least.

    on category archive pages, wordpress generates incorrect database queries, specifically ignoring orderby types of commands.

    it happens even on very simple category.php template files:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Yes, there are a number of print_r statements in there for debugging.

    It generates the following in $wp_query:
    [request] => SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (25,70,71,72,73) ) AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’) GROUP BY wp_posts.ID

    the array that becomes the query string is :
    Array
    (
    [category_name] => base-category/child-category (these are renamed but this is the structure)
    [meta_key] => post-order
    [orderby] => meta_value_num
    [order] => ASC
    )

    I have tested passing both ASC and DESC into the query string. No change. It also ignores order by.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter majestictreefrog

    (@majestictreefrog)

    correct DB query should be something like

    SELECT   wp_posts.* FROM wp_posts  INNER JOIN wp_term_relationships
    ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN
    wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND
    (wp_term_relationships.term_taxonomy_id IN (25,70,71,72,73) ) AND
     wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR
    wp_posts.post_status = 'private') AND wp_postmeta.meta_key =
    'post-order' GROUP BY wp_posts.ID  order by 0+wp_postmeta.meta_value

    Thread Starter majestictreefrog

    (@majestictreefrog)

    FINE. here is pastebin:
    https://pastebin.com/QVpNeB4p

    Thread Starter majestictreefrog

    (@majestictreefrog)

    areas where things could be going horribly wrong.

    wordpress core, query.php.

    When running the code above (and varieties such as testing the query after I merge with $newOrder) I have noticed that only SOME of the variable array seems to be successfully parsed and added to the DB query string.

    I note that meta-key of post-order is queried correctly, but the orderby clause is not. The code where this is parsed is somewhere around line 2267 of query.php in wordpress core.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Category Archives generating wrong DB query and ignoring orderby commands’ is closed to new replies.