WordPress/WooCommerce:将product_variation添加到搜索参数正在提取草稿产品

发布于 2025-01-22 16:38:30 字数 616 浏览 0 评论 0原文

我有一家WooCommerce商店要化妆,我们需要做到这一点,以便可以搜索变化名称,因为人们通常会寻找特定的阴影名称。我正在使用这一点代码来使产品变化可以搜索:

add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );

function search_woocommerce_product_variations( $query ) {
    if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
    }
}

编辑以添加:我已经尝试添加此信息,而搜索结果没有更改:

$query->set('post_status', array('publish')); 

问题是,在以变化的名称搜索时,它正在拉动。 UP在变体名称中具有该搜索词的产品,但目前已设置为草案状态。我如何防止这种情况发生?

I have a WooCommerce store for makeup, and we need to make it so that variation names are searchable, since people will often be looking for a specific shade name. I'm using this bit of code to make the product variations searchable:

add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );

function search_woocommerce_product_variations( $query ) {
    if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
    }
}

Edited to add: I've already tried adding this in, with no changes in the search results:

$query->set('post_status', array('publish')); 

The problem is that when searching on the name of a variation it's pulling up products that do that have that search term in the variation name, but are currently set to Draft status. How can I prevent that from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪失控 2025-01-29 16:38:30

告诉查询您要寻找的post_status:

add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );

function search_woocommerce_product_variations( $query ) {
    if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
        $query->set('post_status', array('publish'));  
    }
}

Tell the query which post_status your looking for:

add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );

function search_woocommerce_product_variations( $query ) {
    if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
        $query->set('post_status', array('publish'));  
    }
}
抚你发端 2025-01-29 16:38:30
<?php
$args = array(
    'post_type' => 'product',
    'numberposts' => -1,
);
$products = get_posts( $args );


foreach($products as $product):
$product_s = wc_get_product( $product->ID );
if ($product_s->product_type == 'variable') {
    $args = array(
        'post_parent' => $plan->ID,
        'post_type'   => 'product_variation',
        'numberposts' => -1,
    );
    $variations = $product_s->get_available_variations();
    echo '<pre>';
    print_r($variations);
    echo '</pre>';
}
endforeach;
?>
<?php
$args = array(
    'post_type' => 'product',
    'numberposts' => -1,
);
$products = get_posts( $args );


foreach($products as $product):
$product_s = wc_get_product( $product->ID );
if ($product_s->product_type == 'variable') {
    $args = array(
        'post_parent' => $plan->ID,
        'post_type'   => 'product_variation',
        'numberposts' => -1,
    );
    $variations = $product_s->get_available_variations();
    echo '<pre>';
    print_r($variations);
    echo '</pre>';
}
endforeach;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文