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

WordPress站点需求:首页隐藏Expired Deals帖子,仅分类筛选时可见

Solution for Hiding Expired Deals Posts from Homepage (WordPress)

Alright, let's get this sorted for your site. You want Expired deals posts hidden from the homepage by default (but not deleted), and only show up when users select that category via your filter bar. Here are two solid approaches depending on how you track "expired" status:

Method 1: Hide the Entire Category from Homepage (Except When Filtered)

If you just want all posts in the "Expired deals" category hidden from the homepage unless the user explicitly selects it, use the pre_get_posts hook to modify the main query.

  1. First, grab the ID of your "Expired deals" category:

    • Go to Posts > Categories in your WordPress admin
    • Click "Edit" on the Expired deals category
    • Look at the URL, you'll see something like tag_ID=123 – that number is your category ID.
  2. Add this code to your theme's functions.php file (or a custom plugin, which is safer for theme updates):

    add_action('pre_get_posts', 'hide_expired_deals_from_home');
    function hide_expired_deals_from_home($query) {
        // Only target the main query on the homepage, and exclude admin area
        if ($query->is_home() && $query->is_main_query() && !is_admin()) {
            // Get the selected category from the filter bar (assuming your filter uses the 'cat' parameter)
            $selected_cat = isset($_GET['cat']) ? intval($_GET['cat']) : 0;
            $expired_cat_id = 123; // Replace this with your actual Expired deals category ID
    
            // If the user didn't select the Expired category, exclude it from the homepage
            if ($selected_cat !== $expired_cat_id) {
                $query->set('category__not_in', array($expired_cat_id));
            }
        }
        return $query;
    }
    
    • Replace 123 with your actual Expired deals category ID
    • This checks if the user is on the homepage and hasn't selected the Expired deals category via the filter bar (using the standard cat URL parameter). If so, it excludes that category from the main query.

Method 2: Hide Posts Based on Expiration Date (More Precise)

If your Expired deals posts have a specific expiration date (not just being in the category), use a custom field to track the expiration date, then filter posts to only show those that haven't expired (unless the user selects the category).

  1. Add a custom field to your posts (use a plugin like Advanced Custom Fields or just the default custom fields):

    • Create a field named expiration_date and set it to a date format (e.g., YYYY-MM-DD)
  2. Update the pre_get_posts hook to check both the category and expiration date:

    add_action('pre_get_posts', 'hide_expired_posts_from_home');
    function hide_expired_posts_from_home($query) {
        if ($query->is_home() && $query->is_main_query() && !is_admin()) {
            $selected_cat = isset($_GET['cat']) ? intval($_GET['cat']) : 0;
            $expired_cat_id = 123; // Replace with your category ID
    
            if ($selected_cat !== $expired_cat_id) {
                // Exclude the Expired category AND hide posts that are past their expiration date
                $meta_query = array(
                    'relation' => 'OR',
                    array(
                        'key' => 'expiration_date',
                        'value' => date('Y-m-d'),
                        'compare' => '>=',
                        'type' => 'DATE'
                    ),
                    array(
                        'key' => 'expiration_date',
                        'compare' => 'NOT EXISTS' // Include posts without an expiration date
                    )
                );
                $query->set('category__not_in', array($expired_cat_id));
                $query->set('meta_query', $meta_query);
            }
        }
        return $query;
    }
    
    • This way, even if a post is in another category but has passed its expiration date, it won't show on the homepage. Only when the user selects Expired deals will all posts in that category (including expired ones) appear.

Notes

  • Always back up your functions.php file before adding code, or use a custom plugin to add snippets (to avoid losing changes when updating your theme)
  • If your filter bar uses a different parameter than cat (e.g., category), adjust the code to use that parameter instead of $_GET['cat']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:24:00