0

In my site I've does some coding based on the category like in_category('featured') do something. Now, this is only applicable to posts only. Also the posts which is associated with featured category is also associated with other category too. As it is only associated with featured category to show up with special design thats it.

Now I know that this question has been discussed before here many time, but not exactly what I'm looking for. So before downgrading this question, red it properly.

Here in this post is there a quick way to hide category from everywhere? the following function is provided to exclude some categories, but I don't want to exclude it I just want to hide it.

add_action('pre_get_posts', 'wpa_31553' );
function wpa_31553( $wp_query ) {

    //$wp_query is passed by reference.  we don't need to return anything. whatever changes made inside this function will automatically effect the global variable

    $excluded = array(272);  //made it an array in case you need to exclude more than one

    // only exclude on the front end
    if( !is_admin() ) {
        $wp_query->set('category__not_in', $excluded);
    }
}

As an example let's say a post is associated with featured category and also associated with Work category. Now as featured is alphabetically comes first the breadcrumb (using Yoast SEO Breadcrumb) will show featured as category name also the post meta (shown below the posts) will show up both category names i.e. featured and work.

That is why I want to hide the featured category completely so that no front end user will ever have idea that featured category exists. It wont show up in breadcrumb, post meta, post category list, no where. It will remain hidden but in the backend code it will still work when I try to do specific with those posts inside featured category using in_category('featured').

Does anyone know how to obtain this category hiding (not excluding) feature.

2 Answers 2

1

Replace all the call to in_category('featured') with the custom function inCategory('featured'), declare it on your functions.php:

/**
 * @param string $category
 *
 * @return bool
 */
function inCategory($category)
{
    global $wpdb, $post;

    if ( ! $post) {
        return false;
    }

    $query = $wpdb->prepare("SELECT COUNT(t.term_id)
    FROM $wpdb->terms AS t
    INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id
    INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
    WHERE tr.object_id = %d
    AND tt.taxonomy = '%s'
    AND t.slug = '%s'
    ", $post->ID, 'category', $category);

    return (bool) $wpdb->get_var($query);
}

The add the following filters:

/**
 * @param array $terms
 *
 * @return array
 */
function remove_featured_category_from_frontend(array $terms)
{
    if ( ! is_admin()) {
        $terms = array_filter($terms, function ($term) {
            if ($term->taxonomy === 'category') {
                return $term->slug !== 'featured';
            }

            return true;
        });
    }

    return $terms;
}

add_filter('get_terms', 'remove_featured_category_from_frontend');
add_filter('get_object_terms', 'remove_featured_category_from_frontend');

Give it a try and let me know.

3
  • Hi @MiCc83 thanks for your reply. With this snippet all the changes I've made in my theme using in_category('featured') has stopped working afted adding that code also in the post category dropdown it still shows up. See: puu.sh/kj2Zr/8418515754.png
    – iSaumya
    Commented Sep 21, 2015 at 13:36
  • WordPress has two way to pick categories. When retrieving all categories it uses get_terms() while when getting categories for a given post it uses wp_get_object_terms(). Replace 'get_object_terms' with 'get_terms' in the code above and let me know if it does the trick. Commented Sep 22, 2015 at 7:41
  • using get_terms does remove the category name from the drop down list but it is now showing in post meta and in breadcrumbs. Take a look - puu.sh/kk217/db608665b2.png and puu.sh/kk21L/d662071d3a.jpg
    – iSaumya
    Commented Sep 22, 2015 at 8:57
0

this is the easiest method

 if($category->name == 'genel') continue;
1
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem? Where exactly would that code go in the question?
    – fuxia
    Commented Feb 5, 2021 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.