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.