Support » Plugins » Custom query by category AND tag doesn’t work?

  • Resolved Matthew Gerring


    This works-

    $my_query = new WP_Query('tag=foo');

    And this works-

    $my_query = new WP_Query('category_name=bar');

    But not this-

    $my_query = new WP_Query('tag=foo&category_name=bar');

    I remember hearing something about categories and tags being the same thing in WP 2.7, so I tried this-

    $my_query = new WP_Query('tag=foo+bar');

    And that doesn’t work either.

    Any suggestions?

Viewing 2 replies - 1 through 2 (of 2 total)
  • and yes I am aware that I need to use an actual ampersand and not &

    If anyone’s interested, here’s my solution.

    Apparently, cross-referencing tags and categories is impossible without a custom SELECT query, since both are stored in the same database. But instead of having a giant, unwieldy SELECT query in my template files, I stuck it into a custom function that I can re-use later, like this-

    function fwpt($cat, $tag)
    global $wpdb;
    $yourCategory = $cat;
    $yourTag = $tag;
    $querystr =	"
    			SELECT p.* from $wpdb->posts p, $wpdb->terms t, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t2, $wpdb->term_taxonomy tt2, $wpdb->term_relationships tr2
    			WHERE = tr.object_id
    			AND t.term_id = tt.term_id
    			AND tr.term_taxonomy_id = tt.term_taxonomy_id
    			AND = tr2.object_id
    			AND t2.term_id = tt2.term_id
    			AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
    			AND (tt.taxonomy = 'category' AND tt.term_id = t.term_id AND t.slug = '$yourCategory')
    			AND (tt2.taxonomy = 'post_tag' AND tt2.term_id = t2.term_id AND t2.slug = '$yourTag')
    return $querystr;

    And then referenced it where I needed it like this-

    if (is_tag()) {
    		$tagname = get_query_var('tag');
    		$taxquery = fwpt('foo',$tagname);
    		$pageposts = $wpdb->get_results($taxquery, OBJECT);
    		foreach ($pageposts as $post):
    		include (TEMPLATEPATH.'/stuff_to_do.php');
    		} else { 
    		$deliverables = new WP_Query('category_name=foo');
            while ($deliverables->have_posts()) : $deliverables->the_post();
    		include (TEMPLATEPATH.'/stuff_to_do.php');

    Cumbersome, yes, but it gets the job done.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom query by category AND tag doesn’t work?’ is closed to new replies.