Forums

[resolved] Getting query_post(array('category__and' => to work with variables not values? (28 posts)

  1. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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?

  2. MichaelH
    moderator
    Posted 2 months ago #

    $filter = array (1,2,3);
    query_posts(array('category__and' => $filter));

  3. Otto42
    Moderator
    Posted 2 months ago #

    Using a variable array will work just fine. Double-check to see what's in your variable just before you use it.

  4. jasoncarlmorgan
    Member
    Posted 2 months ago #

    the array is made like so

    $filter = array();

    if($city != 0)
         $filter[] = $city;
    if($route != 0)
         $filter[] = $route; 
    if($vehicle != 0)
         $filter[] = $vehicle;  

    // Where variables city, route, vehicle are retrieved from $_POST

    therefor, is this not already doing:

    $filter = array(1,2,3);

    anyway?

  5. t31os_
    Member
    Posted 2 months ago #

    Like otto said, check what's in your variable..

    $var = '1'; // Value is a string
    $var = 1; // Value is numeric/integer

    You could try intval on your values..

    $var = intval($var);

    So if $var was '1' (with commas), then intval() would give us just the integer value...

    You check the values using a few basic PHP functions..

    if(is_string($var)) {
    // do something
    }
    if(is_numeric($var)) {
    // do something
    }
  6. t31os_
    Member
    Posted 2 months ago #

    You need an array without keys to use in category__and.. (i think)

    You could do it like this (i think)....

    $filter = array(implode(',',$filter));

    So this..

    Array(
    [0] => 'value',
    [1] => 'value',
    [2] => 'value'
    )

    becomes..

    Array(
    'value',
    'value',
    'value'
    )
  7. jasoncarlmorgan
    Member
    Posted 2 months ago #

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

    Just tried the following but returns NO POSTS!

  8. jasoncarlmorgan
    Member
    Posted 2 months ago #

    Whats bizzare is this:

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

    As this works, because im having to make do with it but the problem is - in its current method it shows posts from EITHER not from ONLY!

  9. t31os_
    Member
    Posted 2 months ago #

    Array the imploded result seperately, it might not like doing it all at once...

    $filter = implode(',', $filter);
    $filter = array($filter);

    Does that help?

  10. jasoncarlmorgan
    Member
    Posted 2 months ago #

    I'm out at the Chinese restaurant! - Will let you know later when I get home!

    Cheers for the help!

  11. Otto42
    Moderator
    Posted 2 months ago #

    Do this for me:

    if($city != 0)
         $filter[] = ($city);
    if($route != 0)
         $filter[] = ($route);
    if($vehicle != 0)
         $filter[] = ($vehicle); 
    
    var_dump($filter);

    What's in $filter?

  12. jasoncarlmorgan
    Member
    Posted 2 months ago #

    Hello guys, back from evening out. Meal was lovely!

    Anyhow, Otto42 here's the output from what you've asked.

    array(3) { [0]=> int(1042) [1]=> int(1192) [2]=> int(1055) }

    That is the result. My category ID's are very high - this is normal.

    Many Thanks

  13. jasoncarlmorgan
    Member
    Posted 2 months ago #

    t31os_ - cheers for the thought, tried as suggested but returns No Posts! - just like all the others! Sorry mate, worth a try though, thanks.

  14. Otto42
    Moderator
    Posted 2 months ago #

    Okay. That's fine.

    So, does this work?
    query_posts(array('category__and' => array(1042,1192,1055)));

    If so, how about this?

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

    Should be no difference, right?

  15. jasoncarlmorgan
    Member
    Posted 2 months ago #

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

    does'nt work!!! - havent tried that next bit, is there point?

  16. jasoncarlmorgan
    Member
    Posted 2 months ago #

    strange how this works:

    query_posts('cat=' . $example); // where $example holds the Cat ID's.

    and how this doesnt:

    query_posts(array('category__and' => $example)); // where $example holds the Cat ID's.

  17. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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

  18. jasoncarlmorgan
    Member
    Posted 2 months ago #

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

  19. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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

  20. t31os_
    Member
    Posted 2 months ago #

    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).
    http://lists.automattic.com/pipermail/wp-trac/2009-May/048874.html

    What version are you on?

  21. jasoncarlmorgan
    Member
    Posted 2 months ago #

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

  22. jasoncarlmorgan
    Member
    Posted 2 months ago #

    Am I right in saying this is the fix?

    http://core.trac.wordpress.org/changeset/11501

  23. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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.

  24. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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.

  25. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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.

  26. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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.

  27. jasoncarlmorgan
    Member
    Posted 2 months ago #

    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!

  28. t31os_
    Member
    Posted 2 months ago #

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

Reply

You must log in to post.

About this Topic