WordPress.org

Forums

Filter posts by is_category (6 posts)

  1. Jannes
    Member
    Posted 5 years ago #

    Hey

    I would like to exclude posts from the frontpage if it was posted in a single specific category, but not if there is more than one category active.

    I will add this to the loop:
    <?php if not (is_category(array(1,2,3)) {query_posts('cat=-4');} ?>

    Happy to hear suggestions on syntax/improvements/standards.

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    The syntax for if not is_category() is if(!is_category(). Note the '!'. It changes the IF to IF NOT.

    The problem I still have is understanding what you're trying to achieve.

    <?php if(!is_category(array(1,2,3)) {query_posts('cat=-4');} ?>

    is the same as

    <?php if(!is_category(array(1,2,3,4)) ?>

    Isn't it?

  3. Jannes
    Member
    Posted 5 years ago #

    I keep mixing up the php "!" with sql "not". Sorry about that.

    <?php if(!is_category(array(1,2,3)) {query_posts('cat=-4');} ?>

    Examine the first part: if condition returns true (thus no category 1, 2 or 3) then the query_posts will execute. Because of the minus-sign posts in category 4 will be excluded in the loop.

    I would like to exclude specific posts (in the example it's category 4), but not if there's more than one category to the post.

    And thanks for your quick reply.

  4. MichaelH
    Member
    Posted 5 years ago #

    This is_category is meant to test if the page of posts being displayed is a category archive.

    I would like to exclude specific posts (in the example it's category 4), but not if there's more than one category to the post.

    I'm not sure you are going to get there with query_posts. Might have to use something like $cats = wp_get_post_categories($post->ID); and test that there is only one element returned, and if so display that post.

  5. Jannes
    Member
    Posted 5 years ago #

    I'm sorry, but I don't understand your suggestion. Can you clarify a bit more, please?

    Let me try to explain what I would like: if a post is posted in a specific single category (like "event") I want to hide it from the front page. But NOT if the post has more than one category to it (like "event" & "news"). Was that clear enough?

    Helpful link to the codex.

  6. MichaelH
    Member
    Posted 5 years ago #

    I'm not sure you can just use query_posts to return the posts you want, so suggest you use query_posts to return all posts in category 4, then in your loop, if the post has more than ONE category, ignore the post.

Topic Closed

This topic has been closed to new replies.

About this Topic