You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PHP函数而非WordPress默认函数在首页排除自定义优惠券分类?

Fixing pre_get_posts to Exclude Custom Taxonomy Categories for Coupons

Hey there, I totally get why your original code wasn't working—you're targeting WordPress's default post category system, but your coupons are a custom post type with a custom taxonomy (the Offer Categories under your Coupons post type). Let's break down how to fix this properly.

Why Your Original Code Failed

The cat parameter in your code only works with WordPress's default category taxonomy. Since your coupon categories are a custom taxonomy (let's assume its slug is offer_category—we'll confirm this later), you need to use tax_query instead to filter posts by this custom taxonomy.

Solution 1: If You're Using the Main Query (pre_get_posts)

If your shortcode relies on the main WordPress query to pull coupons, modify your pre_get_posts function to target the custom post type and taxonomy:

function exclude_offer_categories_from_home( $query ) {
    // Only target front-end, home page, main query, and your coupon post type
    if ( ! is_admin() && $query->is_home() && $query->is_main_query() && $query->get( 'post_type' ) === 'coupon' ) {
        // Build the tax query to exclude the target category
        $tax_query = array(
            array(
                'taxonomy' => 'offer_category', // Slug of your custom taxonomy (Offer Categories)
                'field'    => 'term_id',        // Filter by category ID
                'terms'    => array( 5 ),       // ID of the category to exclude
                'operator' => 'NOT IN',         // Exclude posts from this category
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
    return $query;
}
add_action( 'pre_get_posts', 'exclude_offer_categories_from_home' );

Solution 2: If You're Using a Custom WP_Query in Your Shortcode

If your shortcode uses a custom WP_Query (e.g., new WP_Query( $args )), the pre_get_posts hook won't affect it—you need to add the tax_query directly to your shortcode's query arguments:

function display_all_coupons_shortcode() {
    // Query arguments including taxonomy exclusion
    $args = array(
        'post_type'      => 'coupon',          // Slug of your coupon post type
        'posts_per_page' => -1,                // Fetch all coupons
        'tax_query'      => array(
            array(
                'taxonomy' => 'offer_category',
                'field'    => 'term_id',
                'terms'    => array( 5 ),
                'operator' => 'NOT IN',
            )
        )
    );

    $coupons_query = new WP_Query( $args );

    // Loop through coupons and output your markup here
    $output = '';
    if ( $coupons_query->have_posts() ) {
        while ( $coupons_query->have_posts() ) {
            $coupons_query->the_post();
            // Add your coupon display code (e.g., get_the_title(), coupon details)
            $output .= '<div class="coupon-item">' . get_the_title() . '</div>';
        }
        wp_reset_postdata();
    }

    return $output;
}
add_shortcode( 'all_coupons', 'display_all_coupons_shortcode' );

How to Confirm Your Slugs

To make sure you're using the correct slugs for your custom post type and taxonomy:

  • Go to your WordPress dashboard, navigate to Coupons > Offer Categories
  • Edit any category, and check the browser address bar—you'll see something like:
    edit-tags.php?taxonomy=offer_category&post_type=coupon
  • Here, offer_category is your taxonomy slug, and coupon is your post type slug (replace these in the code if yours are different).

Just swap in your actual slugs and the category ID you want to exclude, and this should work perfectly for filtering your homepage coupons!

内容的提问来源于stack exchange,提问作者Ibrar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:12:50