WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: WordPress Popular Posts] Category exclusion feature is broken; here's the fix (7 posts)

  1. Raam Dev
    Member
    Posted 4 years ago #

    The Category exclusion feature in this plugin is broken. The SQL query used to filter posts by the excluded categories is incorrect (line 554 of wordpress-popular-posts.php):

    $exclude = " AND $wpdb->posts.ID NOT IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (".$instance['exclude-cats']['cats'].")) ";

    The term_taxonomy_id is NOT the same as the category ID. The term_relationships table only references the term_taxonomy table, which is the table that actually includes the category ID (called term_id).

    The real query needed to make this work is a bit hairy, but after a few hours of figuring out how WordPress categories are stored and testing and modifying code, I've come up with a working solution:

    $exclude = " AND $wpdb->posts.ID NOT IN (
    
    SELECT  object_id
    FROM    $wpdb->term_relationships AS r
            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
    WHERE   x.taxonomy = 'category'
            AND object_id IN
               (
                SELECT object_id
                FROM $wpdb->term_relationships AS r
                JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
                JOIN $wpdb->terms AS t ON t.term_id = x.term_id
                WHERE   x.taxonomy = 'category'
                AND t.term_id IN  (".$instance['exclude-cats']['cats']."))) ";

    This was not without the help of code from Damir Sudarevic on this StackOverflow post.

    http://wordpress.org/extend/plugins/wordpress-popular-posts/

  2. Héctor Cabrera
    Member
    Posted 4 years ago #

    Thank you, raamdev!

    Contributions like yours only help make my plugin better for everyone! I'll test this modification on the dev version of WordPress Popular Posts and hopefully include it on the next release.

    The only thing that bothers me is that with the current query seems to be working alright on my test blog with Kubrik's theme. Why does it break on yours?

  3. Héctor Cabrera
    Member
    Posted 4 years ago #

    Update: just integrated your fix with WPP's code and it seems to be running alright!

    I'm by no means a SQL guru, so if anyone is willing to test this fix I'd be happy to hear your feedbacks before introducing it into the next official version of WordPress Popular Posts.

  4. Raam Dev
    Member
    Posted 4 years ago #

    You're welcome, Ikki24!

    I'm not sure why the current query works on your test blog. You might want to take a look at the database for your test blog and see if these tables exist:

    wp_terms - The category and tag IDs with their names, slugs, and descriptions.

    wp_term_taxonomy - The table that describes whether a specific term is a category or a tag.

    wp_term_relationships - The table that ties term_taxonomy's to actual post IDs.

    However, if my submitted fix works, your test blog MUST have these tables.

    One other thing to check is to see if the wp_term_relationships table has term_taxonomy_id's that directly match all of the term_id's (the category IDs) in the wp_terms table.

    It's possible (and I think this is the most likely scenario) that because your test blog is so simple, with so few posts/categories/tags, the term_taxonomy_id's match the the term_id's (the category IDs) exactly and so the filtering works as you'd expect.

    If this turns out to be the case, I recommend you populate your test blog with a lot more posts, categories, and tags, and also try deleting and adding categories/tags so that everything in the database isn't in exact sequence! You should also assign posts to multiple categories/tags and create a few sub-categories just to mimic real-world as closely as possible.

    Thanks for your work on this plugin. It's exactly what I was looking for (especially the exclude categories feature!).

  5. Héctor Cabrera
    Member
    Posted 4 years ago #

    It's possible (and I think this is the most likely scenario) that because your test blog is so simple, with so few posts/categories/tags, the term_taxonomy_id's match the the term_id's (the category IDs) exactly and so the filtering works as you'd expect.

    You could be right about this. Since I'm not running a live blog right now due to lack of time, there might be a few scenarios I haven't contemplated. Fortunately, users like you often point out areas of my plugin that can be improved in benefit of the whole WordPress community, and I'm quite thankful for that.

    Anyways, I'll run some tests on my dummy blog and will release WPP v.2.0.4 within the next few days.

    Thanks again for all the help!

  6. realjoshlee
    Member
    Posted 3 years ago #

    Thanks this is great but where do you actually tell the plugin to exclude a category?

  7. Raam Dev
    Member
    Posted 3 years ago #

    When you add the WordPress Popular Posts widget, you'll have the option of configuring it to exclude specific categories.

Topic Closed

This topic has been closed to new replies.

About this Topic