• Resolved jasoncarlmorgan

    (@jasoncarlmorgan)


    Hello Guys

    I’ve a new issue, tried for awfully long last night to get this to work, but with no success I need further guidance.

    Basically the wordpress ‘query_posts’ codex page says use the following to display ONLY posts that belong to ALL of the defined categories.

    query_posts(array(‘category__and’ => array(2,6)));

    As we know this will display posts that ARE ONLY in both categories 2 and 6.
    … Fair enough, very simple.

    However, in my situation the categories are defined from a form with option values (containing cat_ID) and selected by a user, then the values put into an array called $filter. The array is made with multiple if statements to verify wether or not an option has been selected.

    Therefore I assumed the following would work:

    query_posts(array(‘category__and’ => $filter));

    .. But, it doesn’t. Any thoughts?

Viewing 12 replies - 16 through 27 (of 27 total)
  • Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Ok! – update:

    I added the following to the start of the script:

    error_reporting(E_ALL);

    This is what resulted:

    Notice: Use of undefined constant city – assumed ‘city’ in /home/wyiana1/public_html/leaguespy.com/pgr/wp-content/themes/connections/catfilter.php on line 4

    Notice: Use of undefined constant route – assumed ‘route’ in /home/wyiana1/public_html/leaguespy.com/pgr/wp-content/themes/connections/catfilter.php on line 5

    Notice: Use of undefined constant vehicle – assumed ‘vehicle’ in /home/wyiana1/public_html/leaguespy.com/pgr/wp-content/themes/connections/catfilter.php on line 6

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    OK – Nevermind, it turns out that has nothing to do with this problem!

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Ok, another finding! This ones a bombshell!

    $filter = array();
    
    if($city != 0)
         $filter[] = $city;
    if($route != 0)
         $filter[] = $route;
    if($vehicle != 0)
         $filter[] = $vehicle;
    if(count($filter > 0))
    {
    	$filter = array(implode(',',$filter));
    	query_posts(array('category__in' => $filter));
    
    }

    The above WORKS! – YES WORKS.

    Therefor, using category__in works but, using category__and doesnt?
    …go figure.

    However it still doesnt sort my problem as i HAVE to be able to use category__and

    I’m surprised this doesn’t work..

    query_posts(array('category__and' => array(1042,1192,1055)));

    I’ve just tested that on my test install but with my category IDs..

    query_posts(array('category__and' => array(86,89)));

    Worked first time for me.

    It would appear you have the problem described here (i could be wrong).
    https://lists.automattic.com/pipermail/wp-trac/2009-May/048874.html

    What version are you on?

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    I’m on the latest 2.8 beta, is this why?

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Am I right in saying this is the fix?

    https://core.trac.www.ads-software.com/changeset/11501

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Ok, so i applied the fix as stated above in changeset/11501

    and im currently using the following method:

    $filter = array();
    
    if($city != 0)
         $filter[] = $city;
    if($route != 0)
         $filter[] = $route;
    if($vehicle != 0)
         $filter[] = $vehicle;
    if(count($filter > 0))
    {
    	$filter = array(implode(',',$filter));
    	query_posts(array('category__and' => $filter));
    
    }

    basically from what i can tell, the fix HAS worked to some degree.
    … BUT,

    category__and now definatley works, however its basically acting like category__in!! – not like category__and is suppose to.

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    further update:

    this works how category__and should:
    query_posts(array('category__and' => array(1042,1192,1055)));

    but this work like category__in NOT how it should:
    query_posts(array('category__and' => $filter)); //where $filter is an array.

    i must be missing something.

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    I’ve got a feeling i know what its doing but unsure how to sort it out:

    query_posts(array('category__and' => $filter)); //where $filter is an array

    i think its ONLY displaying posts from the 1st array value and ignoring the other two.

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Yup, i just proven that statement above by swapping the order in which the array values are created and long behold, the 1st array value displays. The others are being ignored.

    Thread Starter jasoncarlmorgan

    (@jasoncarlmorgan)

    Problem solved, now working perfectly.
    This is how i did it!.

    $values = array(
    (int) $_POST["city"],
    (int) $_POST["route"],
    (int) $_POST["vehicle"],
    );
    
    if($values[0] == 0)
       $values[0]='';
    
    if($values[1] == 0)
       $values[1]='';
    
    if($values[2] == 0)
       $values[2]='';
    
    $values = array_filter($values);
    $values = array_values($values);
    
    query_posts(array('category__and' => $values));
    endif ?>

    What a relief this is. The best part of two days working on this.
    Many thanks to all who contributed!

    Glad to see you got it working..

    Using (int) on your post values will expect only integer values… it may be better to check if it’s an integer first if that’s what you expect to be there.

    Perhaps a small function that iterates over the $_POST values, you could then use the function as a callback for the array_filter..

    NOTE: array_filter will remove items with a 0 value (not key), so you can drop the if($values[ etc.. array_filter will remove the zero value items from the array for you.

    I’m not sure how this line helps at all either..

    $values = array_values($values);

    All that will do is rename/reset the array keys (preserving the values).

Viewing 12 replies - 16 through 27 (of 27 total)
  • The topic ‘Getting query_post(array(‘category__and’ => to work with variables not values?’ is closed to new replies.