Forums

[resolved] Cats to exclude array - almost there, just need a quick hint, please! (13 posts)

  1. AlternativePhotography
    Member
    Posted 1 year ago #

    I have a page of "almost" all posts, I want to exclude a few cat and it's children. It DOES exclude the main cats (71 and 71) but DOES NOT exclude the children of 483, 482... am i missing a comma somewhere, or should i use another bit of code?

    $cats_to_exclude = array(
    
      71, // Exhibitions
    
      72,  // Workshops
    
      'child_of' => 483,  // american photographers parent category
    
      'child_of' => 482  // european photographers parent category
    
    );

    Really appreciate a hint from a php expert! :-)

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Have you tried this:

    <?php query_posts(array('category__not_in' => array(71,72)));
     ?>

    I think this will exclude the categories and its children.

  3. AlternativePhotography
    Member
    Posted 1 year ago #

    Hi,
    Thanks for your help. It won't work though, it has a loop underneath fetching the posts from that category... the script below is:

    if ( in_array($category->cat_ID, $cats_to_exclude) ) continue;

    So, i guess i somehow need to make this code above work... is it this bit that is wrong?

    'child_of' => 483,

    Is there another way i can formulate that to make it work?
    Appreciate your help!

  4. AlternativePhotography
    Member
    Posted 1 year ago #

    What the script does is call up the categories, then the posts for each of the categories underneath. Take a look at this page if it makes it clearer:

    http://www.alternativephotography.com/wp/processes/all-articles

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Can't you make the array like this then:

    $cats_to_exclude = array(
    
      71, // Exhibitions
    
      72,  // Workshops
    
      483,  // american photographers parent category
    
      482  // european photographers parent category
    
    );

    If you want to have more child categories in the future you can use something like this so you don't have to hardcode all the child categories in your array:

    $parent_cats_to_exclude = array(71,72);
    foreach ( $parent_cats_to_exclude as $cat  ) {
    	$cats_to_exclude[] = $cat;
    	$child_categories = get_categories('child_of='.$cat);
    	foreach ( $child_categories as $child  ) {
    		$cats_to_exclude[] = $child->cat_ID;
    	}
    }

    Now the $cats_to_exclude array is populated with parent categories and all its child categories

  6. AlternativePhotography
    Member
    Posted 1 year ago #

    Hi,
    Thanks for the advice, but I do want to exclude the child categories as well. So, both parent and child of 483 and 482, 71 and 71 does not have any children.
    How do i write it then? Any ideas?
    Will your script above do this?

  7. AlternativePhotography
    Member
    Posted 1 year ago #

    Yes it does! Thank you so much for your help! I really really appreciate it!

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    How do you mean "483 and 482, 71 and 71 does not have any children."
    I think this is what you need,
    change this:

    $parent_cats_to_exclude = array(71,72);

    to this

    $parent_cats_to_exclude = array(71,72,483,482);

    And let the script do its thing:
    if ( in_array($category->cat_ID, $cats_to_exclude) ) continue;

  9. AlternativePhotography
    Member
    Posted 1 year ago #

    You mean i should change this bit to:
    $parent_cats_to_exclude = array(71,72,483,482);
    And skip this bit?

    foreach ( $parent_cats_to_exclude as $cat  ) {
    	$cats_to_exclude[] = $cat;
    	$child_categories = get_categories('child_of='.$cat);
    	foreach ( $child_categories as $child  ) {
    		$cats_to_exclude[] = $child->cat_ID;
    	}
    }
  10. keesiemeijer
    moderator
    Posted 1 year ago #

    Have you resoved this topic?
    No don't skip that bit. Just put all parent categories in the $parent_cats_to_exclude array. and that bit will make a new array $cats_to_exclude with all parent and children categories you want to exclude

  11. AlternativePhotography
    Member
    Posted 1 year ago #

    Yes, i resolved it. The first bit of script you sent worked fine, i just slotted it in and added 483,482 and it worked fine, like this:

    $parent_cats_to_exclude = array(71,72, 483,482);
    foreach ( $parent_cats_to_exclude as $cat  ) {
    	$cats_to_exclude[] = $cat;
    	$child_categories = get_categories('child_of='.$cat);
    	foreach ( $child_categories as $child  ) {
    		$cats_to_exclude[] = $child->cat_ID;
    	}
    }

    Thank you so much for your help!

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    glad you got it working the way you wanted.

  13. AlternativePhotography
    Member
    Posted 1 year ago #

    Glad you helped me! Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic