Need help with a wordpress-task
I want ALL the posts from sub categories to be excluded.
Example:
- Cake
- Pie
- Apple
- Pear
- Banana
- Pie
If i post a post in Banana, i don't want it to show up in Pie or Cake. I just want posts that are posted in banana to show in banana, not in the top categories.
How can i do this?
I found a code for it to put in functions.php but and it does the trick with the first category, but not the second.
function fb_filter_child_cats($query) {
$cat = get_term_by('name', $query->query_vars['category_name'], 'category');
$child_cats = (array) get_term_children( &$cat->term_id, 'category' );
// also possible
// $child_cats = (array) get_term_children( get_cat_id($query->query_vars['category_name']), 'category' );
if ( !$query->is_admin )
$query->set( 'category__not_in', array_merge($child_cats) );
return $query;
}
add_filter( 'pre_get_posts', 'fb_filter_child_cats' );
Need help with a wordpress-task
I want ALL the posts from sub categories to be excluded.
Example:
- Cake
- Pie
- Apple
- Pear
- Banana
- Pie
If i post a post in Banana, i don't want it to show up in Pie or Cake. I just want posts that are posted in banana to show in banana, not in the top categories.
How can i do this?
I found a code for it to put in functions.php but and it does the trick with the first category, but not the second.
function fb_filter_child_cats($query) {
$cat = get_term_by('name', $query->query_vars['category_name'], 'category');
$child_cats = (array) get_term_children( &$cat->term_id, 'category' );
// also possible
// $child_cats = (array) get_term_children( get_cat_id($query->query_vars['category_name']), 'category' );
if ( !$query->is_admin )
$query->set( 'category__not_in', array_merge($child_cats) );
return $query;
}
add_filter( 'pre_get_posts', 'fb_filter_child_cats' );
Share
Improve this question
edited Sep 25, 2012 at 0:27
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Sep 10, 2012 at 14:15
MwildMwild
311 silver badge3 bronze badges
1
- Could you please mark one of the answers as accepted? Or if you had any different implementation, suggest that and mark it as accepted – Rohit Pande Commented Dec 29, 2015 at 16:40
4 Answers
Reset to default 3Don't change your template, and please do not use query_posts
.
Add this to your function.php
:
add_action('pre_get_posts', 'filter_out_children');
function filter_out_children( $query ) {
if ( is_main_query() && is_category() && ! is_admin() ) {
$qo = $query->get_queried_object();
$tax_query = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $qo->term_id,
'include_children' => false
);
$query->set( 'tax_query', array($tax_query) );
}
}
Easiest way is by using a Category Template.
http://codex.wordpress/Category_Templates
Basically you want to have a category.php page, and then change
<?php while ( have_posts() ) : the_post(); ?>
to this
<?php while (have_posts()) : the_post(); if (in_category($cat)) { ?>
and
<?php endwhile; ?>
to
<?php } endwhile; ?>
I cannot test it right now, but you can try with this code:
$current_cat = intval( get_query_var('cat') );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'category__and' => array($current_cat),
'paged' => $paged,
'caller_get_posts'=> 1
);
query_posts($args);
?>
<?php if (have_posts()) : ?>
Your content - here!
wp_reset_query();
You need to edit the template file where you want to show the posts and put this code.
you can try this, it worked for me
<?php
// Subcategories IDs as an array
// In this example, the parent category ID is 3
$subcategories = get_categories('child_of=3');
$subcat_array = array();
foreach ($subcategories as $subcat) {
$subcat_array[] = '-' . $subcat->cat_ID;
}
// we also include parent category ID in the list
$subcat_array[] = '-3';
// and then call query_posts
query_posts(array('category__not_in' => $subcat_array));
?>
copied from
http://www.maratz/blog/archives/2009/07/13/exclude-articles-from-a-category-tree-on-your-wordpress-homepage/