• I use certain post categories as a way to feed some custom made elements (homepage slider, employee bio widget). I’d like those posts to only be viewable through those elements. Essentially hiding them from every other view including the single post view. I’ve looked at plugins that hide posts from certain areas, but was unable to find one that would hide the single post view.

    Any help is greatly appreciated!

Viewing 15 replies - 1 through 15 (of 25 total)
  • So basically you don’t want the post to be visible in any view that would normally show it? (single, category, tag, archive, etc) What do your custom made elements rely on, if anything, to display these particular posts? (category?) I assume it’s different from making the posts private? Is there any particular plugin that comes closest to what you want?

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I’d only like the posts to be viewable through the widgets I created. Basically, I’m just using a category in order to feed these widgets, so I don’t want the posts in those categories to appear anywhere else, whether it be in the blog list, archives, or single post pages. The plugin that comes closest to what I want is the Ultimate Category Excluder. Thanks for your help!

    OK, cool. I’ll take a look and see what I can come up with (as I have time).

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I really appreciate any help I get! Thank you.

    Add this to your themes functions.php and see if it works for you. It might need a little tweaking, just let me know:

    function exclude_category( $query ) {
        // Add your category id(s) here
        $excluded_category_ids = array(1,17);
        if ( $query->is_main_query() ) {
            if ( ( $query->is_home() || $query->is_category() || $query->is_archive() || $query->is_feed() ) || ( !is_admin() && $query->is_search() ) ) {
                $query->set('category__not_in', $excluded_category_ids);
            } elseif ( $query->is_single() ) {
                $post_categories = wp_get_post_categories( $query->query_vars['p'] );
                foreach ($excluded_category_ids as $category_id ) {
                    if ( in_array( $category_id, $post_categories ) ) {
                        $query->set( 'p', -$category_id );
                        break;
                    }
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Just change:

    $excluded_category_ids = array(1,17);

    to include whatever category ids you want to exclude. Must be an array (but it can contain just one element).

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I will try that and let you know. Thanks!

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I added that bit to my functions file and replaced the ID’s with my own, but to no avail. Is there anything else I could provide to help resolve my issue?

    OK, this works perfectly for me, so let’s try and find what’s different. What works (if anything) and what doesn’t? (I assume the single doesn’t) That is, are your specified posts hidden from category but still show on the single page?

    And have you disabled any plugins you may have tried?

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I just disabled the other plugins, and now it seems the posts are showing up everywhere.

    Add this as the first line of your function:

    echo "<strong>exclude_category called</strong>";

    and see if it shows up at the top of your page. Want to make sure the function is actually getting called.

    If you want to send it to your error log instead, use

    error_log("exclude_category called");

    instead…

    What was happening before you disabled plugins? Was it just not working for the single post page?

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    Yeah, you could still view the single post pages. Ok, so I added the echo and the echo appeared in a BUNCH of places on the homepage.

    OK, let’s try something different. Here’s the most basic code to only prevent your post from showing as is_single (similar to making all your posts for a particular category private).

    So in my case, when I go to my blog page (or category page), then I see the post title. When I click on the link to go to the single post page, I get a 404 (if the post belongs to the excluded category) – ID 1 in my case, which is generally the Uncategorized category by default.

    function exclude_single_post( $query ) {
        $excluded_category_ids = array(1);
        if ( $query->is_single() ) {
            $post_categories = wp_get_post_categories( $query->query_vars['p'] );
            foreach ($excluded_category_ids as $category_id ) {
                if ( in_array( $category_id, $post_categories ) ) {
                    $query->set( 'p', -$category_id );
                    break;
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_single_post' );
    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    I’m not getting the same results. Let me make sure I’m doing a couple of things properly. First, what is the best/easiest way to get a category’s ID? I went to [Dashboard > Posts > Categories > selected Category Name] and then looked in the URL for “tag_ID=21”. Is this correct? Secondly, does it matter what format I’m using for my Permalinks?

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Exclude Single Post View of Certain Categories’ is closed to new replies.