WordPress.org

Ready to get started?Download WordPress

Forums

get_posts - multiple categories & excluding categories (8 posts)

  1. RobotDan
    Member
    Posted 9 years ago #

    I'm trying to get this on the front page:

    a. 1 post from category x
    b. 3 posts from all categories expect x and y
    c. 3 posts from category y

    get_posts can do a. and c. but I can't work out if it can currently exclude categories. Is there a plugin that could do this - maybe something called Enhanced Get Posts :)

    How would I go about taking the current get_posts function and turning it into a plugin that can do a bit more? Thanks for your help!

  2. Lorelle
    Member
    Posted 9 years ago #

    You might find the answer here: http://codex.wordpress.org/The_Loop_in_Action

  3. ColdForged
    Member
    Posted 9 years ago #

    It will exclude a single category with ease -- simply specify a negative category number when using The Loop stuff Lorelle pointed out -- however multiple exclusions are currently not possible. You can however simply specify every category you want to include... lengthy, ugly, but currently the only way to do it short of a core code change (which, if you're brave, you can read about here.)

  4. Root
    Member
    Posted 9 years ago #

    Its not for the faint hearted:)

  5. ColdForged
    Member
    Posted 9 years ago #

    Oh! Or use the plugin I mention in that post! It supports multiple exclusions.

  6. RobotDan
    Member
    Posted 9 years ago #

    Thanks for the help ColdForged! It seems a shame that the get_posts function is almost there... it just needs the ability to exclude categories. At the moment you can specify ONE category for it to display. I have a little experience of PHP, what would be the best way to add this to the function? Hacking the file it's defined in, or creating a plugin based on its code?

  7. ColdForged
    Member
    Posted 9 years ago #

    RobotDan, believe me when I say that to get real multiple-category exclusions -- as opposed to the way that I deal with multiple exclusions whereby, if you want to exclude multiple categories you will only be excluding categories (e.g. you can't do 1 and 3 and 5 but not if they are also in 2 or 4 or 6) -- you are in for a long and frustrating road.

    I tried it at one point and gave up. It's not for lack of PHP or MySQL knowledge, I just don't think it can be done using MySQL 4.0. Here's what I said on the wp-hackers list when I finally gave up... might mean nothing to you, might mean a lot, but it will give you some idea of the battle you face:

    "I admit defeat... I can't get a clean query to save my life. I can only get a query which would theoretically work by using temporary tables as I was fearing. Using that temporary table and the subsequent goofy $whichcat and $join clauses that result break everything else that deals with those clauses. The general gist of the problem is that you can't simply perform a nice binary logical operation to get the needed rows. Well, you could if you could use a subquery with NOT EXISTS, but we want to be nice to the 4.0 MySQL users and so subqueries are out. Why? Because with exclusions you want only post IDs which have _no rows_ that contain an excluded category. Try doing that with a simple AND NOT clause."

  8. RobotDan
    Member
    Posted 9 years ago #

    Going back to my original plan in the first post here, could I use get_posts for a. and c, and then use a category visiblity plugin for b (which will be the standard Loop)? Would this work if I specified the multiple categories to include, and not the 2 to exlcude? Thanks for your help again.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.