• Resolved TwoThirdsWater

    (@twothirdswater)


    Hello all.

    I am writing a custom template to allow me to filter posts in my own way, based on information passed on the query string. This has all been working fine till i get to the point where i want to only see posts that are in both category X AND category Y.

    So.. i start using category__and, but can’t get it to work. If the variable qr_cat is passed in via GET then the page just shows every post in the system not filtered at all. With out the qr_cat variable then all posts are correctly filtered by $blog_category_id var. Now then, I understand that category__and needs to passed an array of category ids to work, but I can’t for the life of me figure out how to do that in this instance.

    All the support i can find is for using it with query_posts not $wp-query. Any help anyone can provide is much appreciated. Revelant section of code below:

    <?php
    $my_query = 'showposts=2&paged=' . $paged . 'orderby=date');
    
    if($_GET["qr_a"])
    {
    	$my_query .= '&author_name=' . $_GET["qr_a"];
    }
    else if($_GET["qr_m"] == "year")
    {
    	add_filter('posts_where', 'filter_more_than_one_year');
    }
    else if($_GET["qr_m"])
    {
    	$my_query .= '&year=' . substr( $_GET["qr_m"], 2, 4 ) . 'monthnum=' . substr( $_GET["qr_m"], 0, 2 );
    }
    
    if($_GET["qr_cat"])
    {
    	$my_query .= 'category__and=' . $blog_category_id . "," . $_GET["qr_cat"]);
    }
    else
    {
    	$my_query .= 'cat=' . $blog_category_id;
    }
    
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query($my_query);
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • I believe you would need your argument structure to use the ‘array’ type argument list. For example:

    $args=array(
          'category__in' => array(5,7,9),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);

    Since you are builiding that argument list in steps, you want to use the array_merge as described in Preserving the Original Query example

    Also note, you should check a value is set and valid before passing it along into a query..

    With your code, you’re leaving an easy way for people to inject malicious code into the query string..

    Additional note:

    $your_query->query( //ARGS );

    Is equivalent to doing..

    query_posts( //ARGS );

    ..the difference being that you are creating a new query, and not using the regular loop..

    Anything you see on the query posts documentation should apply to queries you’re creating using the WP_Query class..

    Thread Starter TwoThirdsWater

    (@twothirdswater)

    Ah! Yes, thank you.

    I had tried the $arg array structure, but then passed the args to $wp_query->query($args);
    not… as you pointed out.
    $wp_query = new WP_Query($args);

    Thanks again.

    T

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using category__and with $wp_query->query’ is closed to new replies.