我想使用帖子 ID 从商店页面隐藏 woocommerce 产品

问题描述 投票:0回答:2

我正在尝试使用帖子 ID 从商店页面隐藏 woo-commerce 产品。有很多不同的方法可以从商店页面隐藏,例如 product_cat 等。

我想通过传递特定的产品ID/帖子ID来从商店页面隐藏特定的产品!!

php wordpress woocommerce
2个回答
1
投票

尝试以下操作:使用“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' );

}

0
投票

如果不起作用;试试这个代码:

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' );
}

这将隐藏商店目录中的产品,但如果需要,允许它们出现在其他页面上。

© www.soinside.com 2019 - 2024. All rights reserved.