I want to create custom links like


So that when a user clicks on the link s/he can see all posts in the category 'ABC' having tag 'XYZ'

For this I've created a custom template with the following code

Template Name: MyCustomTemplate

<?php get_header(); ?>
global $wp_query;
get_query_var( 'cat' );
get_query_var( 'tag' );
<?php get_footer(); ?>

I don't know how to query for the posts in the category 'ABC' with the tag 'XYZ'

I checked http://codex.wordpress.org/Function_Reference/query_posts#Passing_variables_to_query_posts

but the examples shown there use 'static' values. I need to query using dynamic values: which are passed via the URL.

Also, I'm using a plugin 'Advanced Custom Fields' and have added a field 'priority' with the defult value 'Z'. I intend to assign one alphabet to each post in the priority field, so that results on the page are served sorted according to "priority" : Posts with the priority 'A' on the top, followed by posts with priority 'B' and so on..

In Short:

I want to get category and tag parameters from links like:


Then Fetch posts in the category 'some_category' AND having tag 'some_tag' AND sorted according to custom field : 'priority'

  • 1
    Can you just make your question more clear with entering only required information in the question? – Vinod Dalvi Sep 12 '13 at 4:17
  • Sorry for the ambiguous question. I have added a summary in the end of the post. If that doesn't help, please mention what clarifications you need. – Gaurav Srivastava Sep 12 '13 at 6:05
  • FYI, WordPress already possesses the ability to filter post archives on multiple taxonomies without having to run custom queries. see this post. – Milo Sep 12 '13 at 6:32

Clarification of your question:
You want to show Posts of a certain category named "ABC" AND also you want to filter the posts with the tag named "XYZ".

If this is the scenario, have you tried: WP_Query()?

// The Query

$args = array('post_type' => 'post',
              'category_name' => 'ABC',
              'tag' => 'XYZ',
              'meta_key' => 'priority',
              'orderby' => 'meta_value_num'

$the_query = new WP_Query($args);

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
} else {
    // no posts found
/* Restore original Post Data */

Something like this may solve your problem.

NOTE: I din't test this one, try yourself with it. If you face any problem, please comment here, or if you find any solution, don't forget to share that with me here.


  • Thanks. Let me test it. How can I have variable in the array? I mean instead of 'category_name' => 'ABC' I want 'category_name' => $some_category I've added a summary of the question at the end of my post. – Gaurav Srivastava Sep 12 '13 at 6:29
  • Visit the codex page for WP_Query() - I linked earlier, there you will find solution: here. – Mayeenul Islam Sep 12 '13 at 6:37
  • Works! Thank you. Though instead of using 'A', 'B' etc for priority, now I'm using numbers as it makes more sense. The posts with highest 'priority' is displayed on top. Also, on the category page, WP lists all the post of a particular category. Can you suggest how can I make it to display on the basis of priority? So that when someone visits mysite.com/category/ABC posts are displayed according to the priority.. PS: there's a typo. '$' is missing in : new WP_Query(args); I tried to crrect it, but couldn't. :( – Gaurav Srivastava Sep 15 '13 at 16:17

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.