Php Woocommerce从商店页面中排除某些类别

Php Woocommerce从商店页面中排除某些类别,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,试图从我的WooCommerce商店页面中排除单个类别 我正在使用此代码,但它会破坏我网站的属性过滤器链接: 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() ) r

试图从我的WooCommerce商店页面中排除单个类别

我正在使用此代码,但它会破坏我网站的属性过滤器链接:

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( 'tax_query', array(array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
        )));

     }

     remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

您可以使用
woocommerce\u product\u query
hook,它与
pre\u get\u posts
非常相似,只是它已经有了适当的条件逻辑。还有一个
woocommerce\u product\u query\u tax\u query
过滤器,但我不确定它是否存在于woocommerce 2.6中,或者它是否在2.7 beta版中新增

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $q->set( 'tax_query', array(array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'samples' ), 
       'operator' => 'NOT IN'
    )));

}
编辑过滤是通过分类查询完成的,在上面的示例中,我们完全覆盖了税务查询。我现在无法测试这是否有效(数组的数组很复杂,所以我可能弄糟了),但理论是我们需要将新的约束与WooCommerce生成的现有分类查询合并

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

您可以使用
woocommerce\u product\u query
hook,它与
pre\u get\u posts
非常相似,只是它已经有了适当的条件逻辑。还有一个
woocommerce\u product\u query\u tax\u query
过滤器,但我不确定它是否存在于woocommerce 2.6中,或者它是否在2.7 beta版中新增

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $q->set( 'tax_query', array(array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'samples' ), 
       'operator' => 'NOT IN'
    )));

}
编辑过滤是通过分类查询完成的,在上面的示例中,我们完全覆盖了税务查询。我现在无法测试这是否有效(数组的数组很复杂,所以我可能弄糟了),但理论是我们需要将新的约束与WooCommerce生成的现有分类查询合并

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

此解决方案将该类别从商店页面中排除。但问题是,排除类别的类别存档页面显示没有可用的产品

我有我不想在商店页面上登记的电子书类别。我为电子书创建了一个单独的菜单项,在这里我想列出电子书类别中的所有书籍

如何做到这一点

更新

我添加了
if(!$q->is_main_query()| |!is_shop())返回到动作挂钩,它解决了我上面提到的问题。该行仅从商店页面排除该类别,但是,当直接从菜单(类别页面)访问排除的类别时,所有产品都列为良好


add_操作('woocommerce_product_query'、'custom_pre_get_posts_query')

此解决方案将该类别从商店页面中排除。但问题是,排除类别的类别存档页面显示没有可用的产品

我有我不想在商店页面上登记的电子书类别。我为电子书创建了一个单独的菜单项,在这里我想列出电子书类别中的所有书籍

如何做到这一点

更新

我添加了
if(!$q->is_main_query()| |!is_shop())返回到动作挂钩,它解决了我上面提到的问题。该行仅从商店页面排除该类别,但是,当直接从菜单(类别页面)访问排除的类别时,所有产品都列为良好


add_操作('woocommerce_product_query'、'custom_pre_get_posts_query')

我相信商店页面是主查询,而且它肯定是一个post类型的存档,因此这将阻止它在商店存档中工作。@CarlosDaniel…意味着如果(!$q->is_main_query())返回,您应该删除
如果(!$q->is_post_type_archive())返回…:)我相信商店页面是主查询,它肯定是一个post类型的存档,因此这些将阻止它在商店存档中工作。@CarlosDaniel…这意味着如果(!$q->is_main_query())返回,您应该删除
如果(!$q->is_post_type_archive())返回…:)该解决方案可以从主页中排除该类别,但它不允许过滤,这正是我试图实现的。请查看编辑。注意,如果您已经将过滤器小部件设置为“或”关系,我认为您将发现小部件之间不可能存在或关系,并且仍然存在与排除示例的and关系。这不是不可能的,但可能需要您编写一个自定义的
posts\u where
SQL语句,而不是编写分类查询。非常感谢您,代码工作完美,如果遇到任何错误,我会通知您。还将过滤器小部件从“或”更改为“和”,以防止出现任何错误。你是个救命恩人。解决方案可以从主页中排除该类别,但不允许过滤,这正是我试图实现的。请查看编辑。注意,如果您已经将过滤器小部件设置为“或”关系,我认为您将发现小部件之间不可能存在或关系,并且仍然存在与排除示例的and关系。这不是不可能的,但可能需要您编写一个自定义的
posts\u where
SQL语句,而不是编写分类查询。非常感谢您,代码工作完美,如果遇到任何错误,我会通知您。还将过滤器小部件从“或”更改为“和”,以防止出现任何错误。你是个救生员。