1

I feel like I've been all around the web and back again looking for an answer to this and it's really starting to grind my gears.

Not sure if this is the correct way to do it, but I want to add a query_posts array to a URL in the form of a query_arg. This is our query:

query_posts( array( 'meta_key' => 'rank', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged,

How do I use add_query_arg to pass that to the URL so as to re-order the posts with that meta_key/query_posts array? I tried this, it doesn't seem to change the order of the posts, there's something I'm missing here.

<a href="<?php echo $by_rank;?>">  Rank </a>
     <?php $by_rank= esc_url(add_query_arg(array('meta_key' => 'rank',  'orderby' => 'meta_value_num', 'order' => 'DESC'))); ?>

The reason I want to add the query vars to the URL string is so users can sort posts on category pages based on the meta_key/meta_value. Sort of in a similar way to doing ?orderby=date, except with a meta_key.

This can be done, right? Because I'm seriously starting to think it's not possible.

EDIT:- Tried this, this works to sort posts by ASC/DESC order, but not by the meta_key

<?php
$meta_key = (isset($_GET['meta_key'])) ?
            sanitize_text_field($_GET['meta_key']) : 'rank'; // use default value here ''

$orderby = (isset($_GET['orderby'])) ?
           sanitize_text_field($_GET['orderby']) : 'meta_value_num'; // use default value here ''

$order = (isset($_GET['order'])) ?
         sanitize_text_field($_GET['order']) : 'DESC'; // use default value here ''

$by_rank = esc_url(add_query_arg(array(
    'meta_key' => $meta_key,
    'orderby' => $orderby,
    'order' => $order
)));
?>
<a href="<?php echo $by_rank;?>">  Rank </a>

I should add that if I add the query_posts array directly into the page template it works just fine.

6
  • I'm not exactly sure what you're trying to do. You're trying to link to a page (or the same page?) to sort your posts based on a $_GET value?
    – Howdy_McGee
    Mar 30, 2014 at 3:49
  • Yes, pretty much. Same as ordering ?orderby=title except by the meta_key.
    – andy
    Mar 30, 2014 at 4:02
  • Have you tried to use $_GET and then set them in your query? Or pre_get_posts (Codex) perhaps?
    – Howdy_McGee
    Mar 30, 2014 at 4:58
  • I read about using pre_get_posts, but after googling and reading some more I'm not sure as to how I'd roll a function into a query string. Not sure how to use $_GET either :/
    – andy
    Mar 30, 2014 at 5:22
  • If I understand you correctly, I think you want a convenient way to give posts an order ranking so when you run "the loop" they come out in the order you want them. I recommend the most awesome of plugins "Blog Posts Order". Only six lines of code in that puppy. It turns on the attribute metabox so you can add a 'weight' to your posts for sorting. (similiar to a CSS Z-index value.)
    – zipzit
    Mar 30, 2014 at 10:25

1 Answer 1

3
+50

What about something like that?

function wpse139657_orderby(){
    if( isset($_GET['orderby']) ){
        $order = $_GET['order'] or 'DESC';
        set_query_var('orderby', 'meta_value_num');
        set_query_var('meta_key', $_GET['orderby']);
        set_query_var('order', $order);
    }
}

add_filter('pre_get_posts','wpse139657_orderby');

In this way you can call your urls with a ?orderby=rank suffixed and it should do the trick. You can also have an optional order parameter, should you want to implement it.

6
  • Sorry, I'm not seeing the logic as to how this works. Are you suggesting it as an addendum to what I've already layed out in the question or a standalone solution to the url string query? I'm not seeing a meta_key being set here. Or do you merely append the meta_key itself to the URL string?
    – andy
    Apr 2, 2014 at 0:04
  • Oh wait, I see, silly me. Would there be a way to obfuscate the name of the meta_key itself too?
    – andy
    Apr 2, 2014 at 0:10
  • What do you mean by obfuscate? You don't want your users to see what is it called? You can hard-code it, then, and just do an ?orderby=true Apr 2, 2014 at 0:12
  • 1
    Well, can't argue with that answer, 50 points and 1000 internets to you sir.
    – andy
    Apr 2, 2014 at 0:41
  • I believe you will have to award me the internets manually, by the way :) Apr 2, 2014 at 13:00

Your Answer

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

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