0
votes

I want to create custom links like

mydomain.com/custom_page/cat=ABC&tag=XYZ

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

<?php
/*
Template Name: MyCustomTemplate
*/
?>

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

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..

1

1 Answers

0
votes

First of all you are not using get_query_var() correctly. This is a php function and needs to be inside php tags also this function is returning the info that you require so you'll have to save it in a variable. For your example you should use it like this:

<?php
/*
Template Name: MyCustomTemplate
*/
?>

<?php get_header();?>
<?php global $wp_query;
$gotten_cat = get_query_var( 'cat' );
$gotten_tag = get_query_var( 'tag' ); ?>

Now if you fallow a link like mydomain.com/custom_page/cat=ABC&tag=XYZ then $gotten_cat will have the value "ABC" and $gotten_tag will have the value = "XYZ". At some point you need to decide if "ABC" is the category slug or the category id, same with the tag

Now if we assume that ABC is the category id and XYZ is the tag id (if it is the slug there are 2 rows to be added where you get the cat/tag id by it's slug) the code would be like this:

$args = array(
        'cat'      => $gotten_cat, // this uses cat id for cat slug use 'category_name'
        'tag_id'   => $gotten_tag, //this uses tag id for tag slug use 'tag'
        'meta_key' => 'priority',
        'orderby'  => 'meta_value', 
        'order'    => 'ASC',
       );

// run the query
query_posts( $args );

this should query the posts from cat ABC and tag XYZ having meta_key priority set and the posts will be ordered by meta_value (A,B,C...) ascending.

Please read the wp codex page related to WP_Query you will learn how to use parameters with wordpress queries.