首页 > 解决方案 > 我想使用 Post Id 从商店页面隐藏 woocommerce 产品

问题描述

我正在尝试使用 Post Id's 从商店页面隐藏 woo-commerce 产品。有许多不同的方法可以从商店页面隐藏,例如product_cat等。

我想通过传递特定的产品 id/post id 来隐藏商店页面中的特定产品

标签: phpwordpresswoocommerce

解决方案


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

}

推荐阅读