WordPress.org

Ready to get started?Download WordPress

Forums

How to exclude posts from category A except if they also belong to category B (3 posts)

  1. minutepapillon
    Member
    Posted 1 year ago #

    Hi,
    How can I exclude posts from category A except if they also belong to category B ?
    I used query_posts() to entirely exclude a category but it doesn't allow that kind of conditional trick.
    pre_get_posts seems more flexible but I don't know how to build the function to do this very job.
    Any idea is welcome ...
    Thanks.

  2. minutepapillon
    Member
    Posted 1 year ago #

    Well, 5 seconds after posting this I had a simple idea : create a category called "exclude" so that I can exclude on a post by post base just by including the post in this category. My code doesn't work. (I don't do much PHP) Where is my error.
    Thanks

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$cat_id1 = get_cat_ID('actualite');
    	$cat_id2 = get_cat_ID('exclure');
    	query_posts("cat=-$cat_id1,-$cat_id2&paged=$paged"); ?>
  3. minutepapillon
    Member
    Posted 1 year ago #

    ... solved :
    Just one "exclude" category is needed to do all this.

Topic Closed

This topic has been closed to new replies.

About this Topic