Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Exclude child terms posts from parent term posts WordPress

Hi hackers,

I'm trying to get rid of the child terms posts when you visit the parent term in a taxonomy archive page. I made it work, but the next day it stopped. No idea why.

I have made the appropriate testing and code produced the IDs to exclude but seems something is wrong with the filter, but that's just a thought.

This is what I have now:

function ft_mod_where( $where ){
global $wp_query, $wpdb, $wp_version;

// Fire off if we're viewing a category archive
if ( is_tax('menu') ){

// Get children categories of current cat if they exist
$term = get_term_by('slug', $wp_query->get( 'term' ), 'menu');
if ( $excludes = get_categories( "type=dish&taxonomy=menu&child_of=" . $term->term_id ) ) {

// For each child, add just the ID to an array
foreach ( $excludes as $key => $value ) {

$exs[] = $value->term_id;
}

}

// If array exists, remove posts in child categories from query.
if ( isset( $exs ) && is_array( $exs ) ) {

// WP Query changed in 3.1
if ( version_compare( $wp_version, 3.1, '<' ) )
$where .= " AND " . $wpdb->prefix . "term_taxonomy.term_id NOT IN ( ". implode( ",", $exs ) . " ) ";
else
$where .= " AND " . $wpdb->prefix . "term_relationships.term_taxonomy_id NOT IN ( ". implode( ",", $exs ) . " ) ";

}
}

return $where;

}

/**
* Removes the filter after the main query has been built to not interfere with widgets
*
* @since 0.4
*/
function ft_remove_filter() {

remove_filter( 'posts_where', 'ft_mod_where' );

}

if ( ! is_admin() ) {

add_filter( 'posts_where', 'ft_mod_where' );
add_action( 'template_redirect', 'ft_remove_filter' );

}


This is what I head when I made it work but it stopped the next day:

Basically the same: but these lines:

$term = get_term_by('slug', $wp_query->get( 'term' ), 'menu');
if ( $excludes = get_categories( "type=dish&taxonomy=menu&child_of=" . $term->term_id ) ) {


Where this line:
if ( $excludes = get_categories( "type=dish&taxonomy=menu&child_of=" . $wp_query->get( 'term' ) ) ) {

I took this code from a child Category exclusion plugin. Working on-site is an option if required.

PS: the site has no plugins activated and WP_DEBUG set to true - no errors/wornings/notices.

Answers (2)

2011-06-30

Peter Michael answers:

Try this:

function ft_mod_where( $where ) {
global $wp_query, $wpdb, $wp_version;
// Fire off if we're viewing a category archive
if ( is_tax('menu') ) {
// Get children categories of current cat if they exist
$excludes = $exs = array();
$term = get_term_by('slug', $wp_query->get( 'term' ), 'menu');
$excludes = get_categories( "type=dish&taxonomy=menu&child_of=" . $term->term_id );
if ( count( $excludes ) > 0 ) {
// For each child, add just the ID to an array
foreach ( $excludes as $exclude ) {
$exs[] = $exclude->term_id;
}
}
// If array exists, remove posts in child categories from query.
if ( count( $exs ) > 0 ) {
$where .= " AND " . $wpdb->prefix . "term_relationships.term_taxonomy_id NOT IN ( ". implode( ",", $exs ) . " ) ";
}
}
return $where;
}

/**
* Removes the filter after the main query has been built to not interfere with widgets
*
* @since 0.4
*/
function ft_remove_filter() {
remove_filter( 'posts_where', 'ft_mod_where' );
}

if ( ! is_admin() ) {
add_filter( 'posts_where', 'ft_mod_where' );
add_action( 'template_redirect', 'ft_remove_filter' );
}


Baki Goxhaj comments:

No luck. Would you like to try on-site?

2011-06-30

Just Me answers:

Did you echo the new value of $where to check if it is valid? Is the if-loop being executed?


Baki Goxhaj comments:

No I did not. I just echoed the $exs[] value only. I didn't even think of that. Let my try.


Baki Goxhaj comments:

Here is the result:

Warning: Missing argument 1 for ft_mod_where(), called in /hermes/bosweb/web298/b2981/sl.diwilson/public_html/mangostyle/boundry/wp-content/themes/boundry/taxonomy-menu.php on line 7 and defined in /hermes/bosweb/web298/b2981/sl.diwilson/public_html/mangostyle/boundry/wp-content/themes/boundry/functions.php on line 123

Notice: Undefined variable: where in /hermes/bosweb/web298/b2981/sl.diwilson/public_html/mangostyle/boundry/wp-content/themes/boundry/functions.php on line 155

<strong>AND wp_term_relationships.term_taxonomy_id NOT IN ( 72,73,71 ) </strong>


It seems the $where works fine - thant there is something wrong with the query. Right?


Just Me comments:

probably. You should follow the values generated, echo them before loops, inside loops, see what they change into and check if loops are executed.

What happens if you skip the removal of the filter for a moment.