• I am having trouble figuring out how to do a wp_query that will pull posts that are :

    (a) in category 1 or any child-category of category 1,
    AND
    (b) in category 2 or any child category of category 2

    Can’t get this to work… Any help would be apprecaited

Viewing 2 replies - 1 through 2 (of 2 total)
  • nope. category_and and category_in does not pull child cats.
    but theres a hack as mentioned here.

    add this to function.php:

    if ( ! function_exists( 'in_desc_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category' );
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }
    }

    and use it like:

    <?php $myquery = new WP_Query( 'cat=1,2' );
    if( $myquery->have_posts() ) :
    	while( $myquery->have_posts() ) :
    		$myquery->the_post();
    		if ( ( in_category('1') || in_desc_category('1') ) && ( in_category('2') || in_desc_category('2') ) ) { echo ' * '; the_title(); }
    	endwhile;
    endif; ?>

    ^ typo there. function is:

    if ( ! function_exists( 'in_desc_category' ) ) {
    function in_desc_category( $cats, $_post = null ) {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category' );
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can Category_and Pull Child Categories?’ is closed to new replies.