• Resolved gargravarr

    (@gargravarr)


    How do I exclude posts by category in the [wpp] shortcode?

    According to the documentation, there’s no reason why this shouldn’t work:

    [wpp range="custom" time_quantity="7" freshness="0"  order_by="views" taxonomy="category" term_id="-5481" ]

    Now I’m just trying random stuff to try to find a pattern in the error.

    [wpp range="custom" time_quantity="7" time_unit="day" cat="-5481"]
    [wpp range="last7days" freshness="0" cat="-5481" ]
    [wpp range="last7days" freshness="0" order_by="views" cat="-5481" ]
    [wpp range="last7days" freshness="0" order_by="views" taxonomy="category" term_id="-5481" ]

    And dozens of other variants. Am I missing something obvious?

    All sorts of caching is disabled, and clearing wp_popularpoststransients doesn’t help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter gargravarr

    (@gargravarr)

    Found a workaround to override WPP’s query building in functions.php

    function op_wpp_exclude_categories($where, $options) {
    global $wpdb;

    // Check if we have category exclusion in the options
    if (isset($options['cat']) && !empty($options['cat'])) {
    $categories = explode(',', $options['cat']);
    $exclude_cats = array();

    foreach ($categories as $cat) {
    $cat = trim($cat);
    if ($cat < 0) {
    $exclude_cats[] = abs($cat);
    }
    }

    // If we have categories to exclude, add our own WHERE clause
    if (!empty($exclude_cats)) {
    $cat_placeholders = implode(',', array_fill(0, count($exclude_cats), '%d'));

    $where .= $wpdb->prepare(
    " AND p.ID NOT IN (
    SELECT DISTINCT tr.object_id
    FROM {$wpdb->term_relationships} tr
    INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    WHERE tt.taxonomy = 'category' AND tt.term_id IN ({$cat_placeholders})
    )",
    $exclude_cats
    );
    }
    }

    return $where;
    }
    add_filter('wpp_query_where', 'op_wpp_exclude_categories', 10, 2);
    Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @gargravarr,

    According to the documentation, there’s no reason why this shouldn’t work

    Yep, I don’t see anything obvious either. Just tried the very first shortcode you shared on a test site almost as is, only things I changed are the category ID and that I also enabled the display of the category name for testing purposes, and it worked on my end as expected. In my case I’m excluding category ID 14, WordPress, so no posts from that category were listed.

    That wpp_query_where filter hook works -as you already know haha- but I’m still curious to know why the same shortcode worked on my end and not on your site.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exclude by category’ is closed to new replies.