• Resolved rt49andellis

    (@rt49andellis)


    The following is a portion of code from my function.php:

    $recent = new WP_Query();
    $catID = "3";
    $recent->query("cat=$catID");

    $catID = “3” works.
    $catID = “4” works.
    $catID = “23” does NOT work.

    In order to get a double digit variable to work, I have to change the double quotes in $recent->query to single quotes. But, with single quotes, the single digit variable will not work.

    The problem comes when I loop this function and some variables are single digits, some double.

    Thank you for any help you may be able to offer.

Viewing 7 replies - 1 through 7 (of 7 total)
  • $catID is supposed to be an integer – why are you using the quotes?

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Doesn’t $catID = 3; work, without any quotes?

    .. See previous post ?? That.

    Thread Starter rt49andellis

    (@rt49andellis)

    The double/single quote problem still happens even without the quotes on the $catID =

    And, the code actually sets the variable elsewhere. Was hoping the cut down code might help, but it just added more confusion. Here’s the real code

    function listGames($catID) {
    echo "catID is " . $catID . "<br/>"; // works
    $recent = new WP_Query();
    $recent->query("cat=$catID");
    while($recent->have_posts()) : $recent->the_post(); ?>
    .....

    Try

    $recent = new WP_Query( array( 'cat' => $catID ) );
    while($recent->have_posts()) : $recent->the_post();

    If that doesn’t work verify that you actually have posts with that category, etc.

    Thread Starter rt49andellis

    (@rt49andellis)

    I’m not getting that one to work either. I know I’m doing something REALLY dumb!! And now I see there are problems other than the quote problem.

    single.php

    <?php $i = 1;
    $catIDs = array("23","3","4");
    foreach ($catIDs as $catID) {
    echo "loop #" . $i . "<br />";
    listGames($catID);
    echo "<br />---------<br />";
    $i++;
    } ?>

    function.php

    function listGames($catID) {
    $recent = new WP_Query();
    $recent->query( "cat=$catID" );
    while($recent->have_posts()) : $recent->the_post();
    the_title();
    echo "<br />";
    endwhile; }

    OR function.php (from Ron)

    function listGames($catID) {
    $recent = new WP_Query( array( 'cat' => $catID ) );
    while($recent->have_posts()) : $recent->the_post();
    the_title();
    echo "<br />";
    endwhile; }

    output

    loop #1
    POST FROM CAT 4
    POST FROM CAT 3
    POST FROM CAT 23
    
    ---------
    loop #2
    POST FROM CAT 3
    
    ---------
    loop #3
    POST FROM CAT 4
    POST FROM CAT 3
    
    ---------

    Thread Starter rt49andellis

    (@rt49andellis)

    I think that might actually be working correctly and it’s picking up parent/child stuff.

    categories 3&4 are children of 23
    category 3 is a child of 4

    How do I tell it to ignore parents & children?

    Thread Starter rt49andellis

    (@rt49andellis)

    The answer is
    $recent = new WP_Query( array( 'category__in' => $catID ) );

    Thank you EVERYONE for helping me out!!!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘$recent->query of cat problem’ is closed to new replies.