title: Querying by meta keys in wordpress URL string
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>query_posts( array( 'meta_key' =&gt; 'rank', 'orderby' =&gt; 'meta_value_num', 'order' =&gt; 'DESC' , 'paged' =&gt; $paged,
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>&lt;a href="&lt;?php echo $by_rank;?&gt;"&gt;  Rank &lt;/a&gt;
       &lt;?php $by_rank= esc_url(add_query_arg(array('meta_key' =&gt; 'rank',  'orderby' =&gt; 'meta_value_num', 'order' =&gt; 'DESC'))); ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>This can be done, right? Because I'm seriously starting to think it's not possible.</p>
  
  <p>From Rahil's answer</p>
  
  <pre><code>&lt;?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' =&gt; $meta_key,
      'orderby' =&gt; $orderby,
      'order' =&gt; $order
  )));
  ?&gt;
  &lt;a href="&lt;?php echo $by_rank;?&gt;"&gt;  Rank &lt;/a&gt;
  </code></pre>
  
  <p>Put in the values, the same values that work fine with a query_posts array on other pages, posts don't re-order.</p>