Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to get all products from the wpsc-product post type and then display them by a custom meta field called release_date_year. However, it doesn't appear to be ordering my posts by this custom meta value.

I have read through countless blog posts, support posts on the Wordpress forums and a lot of questions and answers here on Wordpress Answers. However, I could not find a solution to my issues.

Here is the code that I am using to get the products and then order them by a custom meta field. Ignore the each_connected argument as that is for the plugin Posts to Posts by Scribu. A quick note: I have also tried remove the each_connected part to see if it fixed the issue, but as I suspected, it didn't change a thing.

It appears as though the below code gets the products okay, that's not the issue. The issue seems to be that they are being ordered by the date they were published and not the custom meta field value which is a numeric value.

    $products = new WP_Query(array(
        'post_type'         => 'wpsc-product',
        'nopaging'          => true,
        'meta_key' => 'release_date_year',
        'each_connected' => array(
            'post_type' => 'artists',
            'nopaging'  => true,
        ),
        'suppress_filters' => false,
        'orderby' => 'meta_value_num',
        'order' => 'asc',
    ));

I then looked at what queries were being run and the following is the result:

SELECT   wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND wp_posts.post_type = 'wpsc-product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND wp_postmeta.meta_key = 'release_date_year' GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC           

Now is it just me or should the 'ORDER BY' part of my query have wp_postmeta.meta_key in it? Because it looks to me it's ordering by the menu order and then the post date descending. The other weird thing is that I am also seeing posts ordered descending instead of ascending.

Like I said, I also tried using meta_query in my WP_Query query as I am using Wordpress 3.1 using the following code and it was still ordering posts by post_date:

$products = new WP_Query(array(
    'post_type'         => 'wpsc-product',
    'nopaging'          => true,
    'meta_key' => 'release_date_year',
    'meta_query' => array(array(
        'key'  => 'release_date_year',
        'type' => 'NUMERIC'
    )),
    'each_connected' => array(
        'post_type' => 'artists',
        'nopaging'  => true,
    ),
    'suppress_filters' => false,
    'orderby' => 'meta_value',
    'order' => 'desc',
));

Have I incorrectly formatted my WP_Query statement or is something else at play here? I have also confirmed there is data in the wp_postmeta table and there are values for each product there.


Update and Solution

After some messing around, I found the culprit. A plugin called 'Post Types Order' which was modifying my Wordpress queries and hence causing the suppress_filters argument to be completely ignored, amongst other things like orderby.

See my answer below.

share|improve this question

2 Answers 2

It looks like your 'orderby' parameter is completely ignored.

Try setting 'suppress_filters' => true to make sure there's no interference.

share|improve this answer
    
Sadly, setting suppress_filters to true did nothing. The query string looks exactly the same using both the standard meta_key and the meta_query approach. Are there any other ways I can sort posts by meta value? I appreciate your help Scribu. I'm thinking a plugin conflict might be occurring. –  DigitalSea Mar 26 '11 at 15:29
    
Argh! I found the culprit. Somehow the plugin 'Post Types Order' was activated and modifying my queries even with suppress_filters set to true which should stop the query being modified right? Anyway, thank you. I'll updated my original answer. What do I do in this situation? Should I just pick your answer, delete it or? –  DigitalSea Mar 26 '11 at 15:33
    
Then that means Post Types Order is filtering the raw SQL query, which is pretty crazy. (Leave the question as is, I guess.) –  scribu Mar 26 '11 at 17:27
    
@Dwayne you should add your solutions as answer and accept it. If possible, provide details how to work around the issue (if that makes sense instead of just ditching plugin at fault). –  Rarst Mar 26 '11 at 18:36
    
Excellent, thank you guys. I cannot answer my own question until tomorrow apparently, but I posted the solution as an answer below and also posted a proposed workaround as well. –  DigitalSea Mar 27 '11 at 6:27
up vote 2 down vote accepted

The plugin 'Post Types Order' was the culprit. As Scribu pointed out above the plugin is filtering the Raw SQL query which it shouldn't be doing. As a result, the argument 'suppress_filters' wasn't stopping the query being modified and the 'orderby' argument was being completely ignored.

As to how this behavior can be stopped, I'm not sure. I solved my issue by disabling the plugin as it wasn't even being used. However, you can set the order of your custom post type menus in your register_post_types code instead, using the 'menu_position' argument which decides where the menu will be placed.

Documentation for ordering your post types in the Wordpress admin menu can be found here in the official Wordpress documentation.

The lesson learned here is that you should always assume that a plugin has broken something, especially if you are using code from the official documentation that works.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.