I would like to change the loop order via a link or form input, but it can`t load via URL, because i need to orderby=meta-value and this cannot be done via url args (acording the codex) , only like this:

<?php $argsvs = array( 
                    'post_type' => 'evento', 
                    'posts_per_page' => -1,
                    'meta_key' => 'wpcf-evento-data-inicio1',
                    'orderby' => 'meta_value',
                    'order'=>'DESC' );
$loop = new WP_Query( $argsvs );

How would I let the user change the above args to orderby another custom field? ex:

                        'meta_key' => 'wpcf-evento-data-inicio2',
                        'orderby' => 'meta_value'

If it was order by title or something like this, i could do it via URL like ?post_type=evento&orderby=title&order=ASC, but for a meta_key and orderby=>'meta_value' args, this can't be done via URL


1 Answer 1


Why not do it via URL parameter? You said you can't because Codex told you not to. But if you want to anyway, you could use a GET parameter:

// http://example.com/page-slug/?direction=DESC
if($_GET['direction'] == 'DESC'){
    $order = 'DESC';
} else {
    $order = 'ASC';

$args = array(
    'order' => $order,
  • Codex doesn't say you shouldn't do this, I guess (at least I don't remember it does). It only says that there is no built in option to do this. Jun 20, 2013 at 19:45
  • Only referencing OP's statement > and this cannot be done via url args (acording the codex)
    – GhostToast
    Jun 20, 2013 at 19:48
  • codex.wordpress.org/WordPress_Query_Vars 'Private query vars cannot be used in the URL, although WordPress will accept a query string with private query vars, the values will not be passed into the query and should be placed directly into the query.' :( that was the main problem. Great awnser btw, is there any security issues related? like sql injection or something Jun 20, 2013 at 19:55
  • Well, not really in this example. Consider what may be passed on that variable direction. We're not blindly accepting any value. We are looking for one specific value and then changing a different variable based on that. Much different than "if something is there, use it no matter what" - get it?
    – GhostToast
    Jun 20, 2013 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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