[wp-hackers] Filter 2 categories with a plugin

Moises Kirsch moi at kirsch.com.mx
Wed Nov 12 07:16:30 GMT 2008


Hi,

I'm trying to create a plugin that will filter two categories...  
(there is one out there allready that does this but I don't like how  
it works because from my point of view it is not using some standard  
functions on wordpress).

According to this post by Ryan Boren

http://boren.nu/archives/2007/10/01/taxonomy-intersections-and-unions/

It should be very simple... but for some reason I can't make it work.  
Here is part of the code that I'm using:

	if($cat1 && $cat2){
		$wp->set_query_var('category__and', array($cat1, $cat2));	
	}

Am I using the set query var correctly?

Thats where it seams to fail right now... I can get the correct  
categories IDs (I'm working with permalinks). I know the if is working  
(I even hardcoded a few IDs to test it out).

The only part that I am not sure about is the set query part.

Im also posting the full code after this (ignore the comments in  
spanish).

Hopefully someone can help me fix this.

Thanks


<code>

<?php
/*
Plugin Name: Dual Cats by Moi
Plugin URI: http://moiblog.com/
Description: Filtra 2 categorias.
Author: Moises Kirsch
Version: 1.0
Author URI: http://moiblog.com/
*/


// Funcion condicional
function is_dual_cats(){
	global $is_dual_cats;
	return $is_dual_cats;
}


add_action('parse_request', 'dual_cats_query' );
function dual_cats_query(/* $cat1 = 16, $cat2 = 25 */){
	global $wp, $is_dual_cats, $wp_query;
			
	$cat1 = get_category_by_slug($wp->query_vars[cat1]);
	$cat2 = get_category_by_slug($wp->query_vars[cat2]);

	$cat1 = $cat1->term_id;
	$cat2 = $cat2->term_id;

	if($cat1 && $cat2){
		$is_dual_cats = true;
		//$wp_query->set('category__and', array($cat1, $cat2));
		$wp->set_query_var('category__and', array($cat1, $cat2));	
	}
	else{
		$is_dual_cats = false;
	}
}


// Reinciar las reglas del URL Rewrite
add_action('init', 'dual_cats_flush_rewrite_rules');
function dual_cats_flush_rewrite_rules()
{
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

// Reglas del URL Rewrite
add_action('generate_rewrite_rules', 'dual_cats_add_rewrite_rules');
function dual_cats_add_rewrite_rules( $wp_rewrite ){
	$new_rules = array('cats/(.+)/(.+)?$' => 'index.php?cat1='. 
$wp_rewrite->preg_index(1).'&cat2='.$wp_rewrite->preg_index(2) );
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}


// Interceptar el template
add_action('template_redirect', 'dual_cats_template_intercept');
function dual_cats_template_intercept(){
	global $wp;

	//print_r($wp);
	
	if(is_dual_cats()){
		if(file_exists(TEMPLATEPATH.'/category.php')){
			include(TEMPLATEPATH.'/category.php');
			exit;
		}
		elseif(file_exists(TEMPLATEPATH.'/archive.php')){
			include(TEMPLATEPATH.'/archive.php');
			exit;
		}
		else{
			include(TEMPLATEPATH.'/index.php');
			exit;
		}
	}

}


// Agregar Variables al Query
add_filter('query_vars', 'queryvars');
function queryvars( $qvars ){
	$qvars[] = 'cat1';
	$qvars[] = 'cat2';
   	return $qvars;
}
?>

</code>

--

Ing. Moises Kirsch Sandler
044 (81) 1044-3216
moises at kirsch.com.mx
www.subjetivo.com



More information about the wp-hackers mailing list