• Resolved tismey

    (@tismey)


    Here’s the setup. I have a ‘current distractions’ category which appears in my sidebar, but doesn’t appear on my main page (using
    if !( in_category(’17’) )
    ). What I’d like to be able to do is have these entries appear on the front page if they also belong to another category – ie exclude them if are ONLY a current distraction and not also of interest for another reason.

    I’m sure someone’s going to tell me this is dead easy and I’ve missed something somewhere but I’ve searched and search…no dice.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Remove the in_category stuff and use this in your root index.php:

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', true);
    if( !isset($cat) ) {
    $cat = '-17';
    }
    require('./wp-blog-header.php');
    ?>

    Thread Starter tismey

    (@tismey)

    Hi there, thanks for the fast response. That’s excellent and does exactly what it says on the tin.

    In the spirit of understanding rather than cutting and pasting, what is that actually doing?

    $cat is an internal variable WP uses to specify the categories to include in the posts collection. However, when a – is prefixed, WP parses this and excludes the category instead.

    Note that there are a couple of proviso’s to this method:

    1. only one category can be excluded this way
    2. posts must belong to only this category. Posts that belong to multiple categories (even if one of them are excluded) are still picked up. This is due to a flaw in the sql code (which itself was due to a workaround for MySQL limitations).
    3. This is, to my knowledge, undocumented. I only found this out by examining the source code.
    Thread Starter tismey

    (@tismey)

    So the only way to do what I wanted to do is with an undocumented feature which is itself the result of a flaw in the SQL? Figures. I do like to make things difficult for myself!

    Thanks for your help!

    Thread Starter tismey

    (@tismey)

    Would this break the ability to browse categories? Now when I click on a category it just give me my front page again. Removing these lines lets me browse by category quite happily.

    Thread Starter tismey

    (@tismey)

    OK, I figured this – using $cat in the root index.php broke category browsing (clicking on a category title just took you back to the homepage rather than to that category).

    After some poking around in the codex I discovered query_posts which appears to have been documented fairly recently. Using
    <?php
    if (is_home()) {
    query_posts("cat=-17");
    }
    ?>

    has the desired effect, without breaking anything.

    I would like this too. Where do I find query_posts or do I have to create that file, and in which directory would it go if that is the case?

    Thread Starter tismey

    (@tismey)

    query_posts is a built-in template tag. The codex entry is at
    https://codex.www.ads-software.com/Template_Tags/query_posts
    I just pasted the above lines into my index.php page, just before the loop.

    This method has 2 disadvantages, I have discovered. You can only exclude one category this way and the posts you exclude using query_posts (or indeed using the first method here, $cat) will still be included in your RSS feed. Another way of doing the same thing without these caveats is to use the RumCategory plugin
    https://rummanddan.dk/plugins/rumcategory/
    which will let you exclude categories from either the front page or the RSS Feed ‘on-the-fly’ using checkboxes on the Edit Category page.

    Nice work Timsey – you’ve made me realise I need to do this too, and I’ll use your code rather than the plugin for the sake of reducing bloat.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to exclude posts that only belong to one category?’ is closed to new replies.