我正在尝试使用帖子 ID 从商店页面隐藏 woo-commerce 产品。有很多不同的方法可以从商店页面隐藏,例如 product_cat 等。
我想通过传递特定的产品ID/帖子ID来从商店页面隐藏特定的产品!!
尝试以下操作:使用“pre_get_posts”钩子
<?php
add_action( 'pre_get_posts', 'erginous_exclude_id' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'post__not_in', array(70, 53) ); // Replace 70 and 53 with your products IDs. Separate each ID with a comma.
}
remove_action( 'pre_get_posts', 'erginous_exclude_id' );
}
如果不起作用;试试这个代码:
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'post__not_in', array(1, 2) );
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
这将隐藏商店目录中的产品,但如果需要,允许它们出现在其他页面上。