0

If I use this code(code not complete, this is the problem section), the posts appear properly. NOTE the "new WP_Query('cat=2,3,5,6,7,8,9');":

$catslugs = array('academic',/*'adventure',*/ 'arts', 'religious', 'special-interest', 'special-needs', 'sports','teen');//Must upate as new Main categories are added 
    $out = array();
    foreach($catslugs as $slug) {
        array_push($out, get_category_by_slug($slug)->term_id);
        $CatIDs= implode(',', $out);
        }
    //featured category ID = 153
    //academic category ID = 2
    //adventure category ID = 4
    //arts category ID = 3
    //religious category ID = 5
    //special-interest category ID = 6
    //special-needs category ID = 7
    //sports category ID = 8
    //teen category ID = 9
    $CatIDsCode="'cat=".$CatIDs."'";
    $camp_posts_bynumber = new WP_Query('cat=2,3,5,6,7,8,9');

    if( $camp_posts_bynumber->have_posts() ):

If I change it to this it doesn't. Instead it shows ALL categories including "Uncategorized". What am I doing wrong? Is it something with "have_posts()"

$catslugs = array('academic',/*'adventure',*/ 'arts', 'religious', 'special-interest', 'special-needs', 'sports','teen');//Must upate as new Main categories are added 
$out = array();
foreach($catslugs as $slug) {
    array_push($out, get_category_by_slug($slug)->term_id);
    $CatIDs= implode(',', $out);
    }
//featured category ID = 153
//academic category ID = 2
//adventure category ID = 4
//arts category ID = 3
//religious category ID = 5
//special-interest category ID = 6
//special-needs category ID = 7
//sports category ID = 8
//teen category ID = 9
$CatIDsCode="'cat=".$CatIDs."'";
$camp_posts_bynumber = new WP_Query($CatIDsCode);

if( $camp_posts_bynumber->have_posts() ):

OR

$catslugs = array('academic',/*'adventure',*/ 'arts', 'religious', 'special-interest', 'special-needs', 'sports','teen');//Must upate as new Main categories are added 
$out = array();
foreach($catslugs as $slug) {
    array_push($out, get_category_by_slug($slug)->term_id);
    $CatIDs= implode(',', $out);
    }
//featured category ID = 153
//academic category ID = 2
//adventure category ID = 4
//arts category ID = 3
//religious category ID = 5
//special-interest category ID = 6
//special-needs category ID = 7
//sports category ID = 8
//teen category ID = 9
$CatIDsCode="'cat=".$CatIDs."'";
$camp_posts_bynumber = new WP_Query($CatIDs);

if( $camp_posts_bynumber->have_posts() ):
0

I'm pretty sure it's because you've got mixed nested quotes when you're setting $CatIDsCode.

So this should fix it:

$camp_posts_bynumber = new WP_Query (array ('cat' => $CatIDs));

P.S. Move $CatIDs= implode(',', $out); outside of the foreach loop. :)

  • Thank you! That did the trick! Working well now. I just don't know all the proper syntax for this stuff. – Kirk Jan 4 '17 at 18:22
  • @Kirk Great, glad it worked. Please accept the answer if that's everything you needed. – iguanarama Jan 4 '17 at 18:31
  • I will, I have another question but will ask it separately. – Kirk Jan 4 '17 at 18:38
  • I clicked the check mark, I assume that is the "accept" button? Sorry, new to this forum. – Kirk Jan 4 '17 at 18:52
  • @Kirk that's it, no problem, saw you were new to it. – iguanarama Jan 4 '17 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.