1

I have an author page which shows all uploaded media by that author. Each media can have tag1, tag2 or tag3. I would like to add an option where I can show on that same page only tag1 media without have to setup a new site structure. So I can up with the idea of using query strings.

My idea: passing a query string like http....url.../author/name?MediaTag=tag1

And use this in my query like <?php query_posts('cat=1&author=' . $post->post_author . '&order=DESC&tag=' . $tag1 .'&posts_per_page=12' . '&paged=' . get_query_var('paged')); ?>

and a check if there is no query string, it uses the one I'm using right now <?php query_posts('cat=1&author=' . $post->post_author . '&order=DESC&posts_per_page=12' . '&paged=' . get_query_var('paged')); ?>

Do I need to do anything else? Like catch the query string? Is this going to work? But never used this and any help is more than welcome. Maybe there is another way.

1

As per the given url http....url.../author/name?MediaTag=tag1, below code may helpful...

if(isset($_GET['MediaTag']))  //It will check the value of MediaTag (from address bar after ?)
{
  $tag1 = $_GET['MediaTag'];  //Assigning value of MediaTag to the variable
  if($tag1)  //Checking if variable have some value or not
  {
    query_posts('cat=1&author=' . $post->post_author . '&order=DESC&tag=' .  $tag1 .'&posts_per_page=12' . '&paged=' . get_query_var('paged'));
  } else {
        query_posts('cat=1&author=' . $post->post_author . '&order=DESC&posts_per_page=12' . '&paged=' . get_query_var('paged'));
 }}
  • Hi Rishabh! Would you mind explaining how your code works? – Tim Malone May 4 '16 at 8:43
  • Hi @Tim Malone Answer have been updated with explanation. – Rishabh May 4 '16 at 9:20
  • works like a charm Rishabh, exactly what I was looking for! – AKNL May 4 '16 at 11:08
  • @AKNL I am glad it worked :) – Rishabh May 4 '16 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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