WordPress.org

Ready to get started?Download WordPress

Forums

Hide Child Category Posts (5 posts)

  1. mystifier
    Member
    Posted 2 years ago #

    For many years, I have used a plug-in to hide child category posts but after a recent upgrade to 3.1.2, this has stopped working and I have not been able to find a replacement that works.

    Can anyone advice how to achieve this?

    I only want posts for the current category to show.

    while (have_posts()) : the_post(); if(in_category($cat)) {

    has been suggested but I don't have $cat and can't work out how to set it to current_category.

  2. Digital Raindrops
    Member
    Posted 2 years ago #

    Not sure but try getting the first category from the post.

    while (have_posts()) : the_post();
    $category = get_the_category();
    if( $category ) {
       $catid = $category->cat_ID;
    }

    Or maybe:

    while (have_posts()) : the_post();
    if( is_category() ) {
       $category = get_the_category();
       $catid = $category->cat_ID;
    }

    HTH

    David

  3. mystifier
    Member
    Posted 2 years ago #

    No joy... I think get_the_category() may need Post_ID as a parameter.

    This is pretty crippling now so I may have to wind back to an early version where plug-ins work and stay there.

    (Admin suffers in the same way. With several layers of sub-category, filter posts by category now returns many tens of pages because it includes every post from every child category - this was a dumb idea!).

  4. mystifier
    Member
    Posted 2 years ago #

    The original plug-in, which has served fantastically for years, was very simply:

    <?php
    function kinderlose_where($where) {
      if ( is_category() ) {
        global $wp_query;
        $where = preg_replace('/.term_id IN \(\'(.*)\'\)/', '.term_id IN (\'' . $wp_query->query_vars['cat'] . '\')', $where);
      }
      return $where;
    }
    
    add_filter('posts_where', 'kinderlose_where');
    ?>

    Is there an obvious reason why this no longer works?

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    No joy... I think get_the_category() may need Post_ID as a parameter

    It gets the first category of the current post unless you pass an ID in, as you are in the loop if there is a category it should return it?

    Missed the [0]
    $category[0]->cat_ID

    David

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.