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.

Ok, so I have this custom field wpcf-evento-data-inicio1 generated with the (great) Types plugin.
This is how it stores on db:
1372896000
OK, so I want to order by this value. I'm doing it by url args, and i can't spot what is wrong... I'm currently doing like this:

mysyte.com/?post_type=evento&order=desc&orderby=meta_value_num&meta_key=wpcf-evento-data-inicio1

I have also tried with meta_value instead of meta_value_num

mysyte.com/?post_type=evento&order=desc&orderby=meta_value&meta_key=wpcf-evento-data-inicio1

it outputs like this order:

  1. 1372896000
  2. 1371600000
  3. 1372204800

I just cannot see what is wrong, any ideas? Thank you so much.

(edit: Pls note that this is a archive template (archive-evento.php)

share|improve this question
    
I just noticed you first wrote wpcf-evento-data-incio1 and then use wpcf-evento-data-inicio1 –  birgire Jun 18 '13 at 13:24
    
@birgire the first one was a typo. eddited. the one in url is right (double checked) –  user1576978 Jun 18 '13 at 13:25
    
@birgire any ideas? thanks for spotting the error :) –  user1576978 Jun 18 '13 at 13:35
    
I'm not sure you can order by meta this way, you can check out public query vars codex.wordpress.org/WordPress_Query_Vars –  birgire Jun 18 '13 at 13:40
add comment

1 Answer 1

up vote 1 down vote accepted

I can't get a meta_key parameter to pass through a GET string at all. Many other parameters that I tested do pass through, though I did not do an exhaustive test, but meta_key does not.

If you want to verify that just add the following to the top of your header.php and inspect the query.

echo '<pre>'; 
var_dump($wp_query); die;
echo '</pre>';

I have never tried to pass that parameter before now but checking the Codex (thanks @birgire), I see that meta_key is listed as a "Private" variable. You cannot pass private variables through an URL, even if the parameters are valid if passed directly to WP_Query via hard-coded PHP.

What you are attempting, seems to be intentionally not allowed. You can only modify the query via an URL using the parameters listed as "Public". Other parameters are ignored, as you can see by inspecting the query as above.

That aside, when I see "ordering" problems like that the first thing I suspect are sticky posts. If you were using WP_Query directly I'd suggest that you add &ignore_sticky_posts=true but I can't get that to pass through GET either.

However, if you really must, you should be able to force this through with a filter on pre_get_posts.

function order_by_meta($qry) {
  if (isset($_GET['meta_key']) && $_GET['meta_key'] == 'wpcf-evento-data-inicio1') {
    $qry->set('meta_key','wpcf-evento-data-inicio1');
  }
}
add_action('pre_get_posts','order_by_meta');
share|improve this answer
    
I really appreciate your time and effor to help me on this. I checked codex.wordpress.org/Class_Reference/WP_Query @ "Order & Orderby Parameters" where 'meta_value' and meta_value_num' are possible arguments, but i can't realize what's wrong. Oh, thats a archive template btw.... –  user1576978 Jun 18 '13 at 14:01
    
oh, I see now "Private query vars cannot be used in the URL," –  user1576978 Jun 18 '13 at 14:03
    
Yes, with WP_Query they are valid parameters, but they are not public and so cannot be passed through an URL like you are apparently trying to do. –  s_ha_dum Jun 18 '13 at 14:03
    
if you could pls edit your awnser with this info i will select it as awnser, to help other users with this same problem –  user1576978 Jun 18 '13 at 14:04
    
My answer already states that meta_key is a "Private" variable. –  s_ha_dum Jun 18 '13 at 14:06
show 4 more comments

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.