亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 中使用 WC_Query 按作者 ID 獲取產品?

在 WooCommerce 中使用 WC_Query 按作者 ID 獲取產品?

PHP
繁花不似錦 2023-10-15 15:50:48
我嘗試使用 WooCommerce 中的 WC_Query 通過帖子作者 id 獲取產品,因此我嘗試包含一個新的自定義元密鑰 "_author",其中包含以下內容:add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handling_custom_meta_query_keys', 10, 3 );function handling_custom_meta_query_keys( $wp_query_args, $query_vars ) {    $meta_key = '_author';     if ( ! empty( $query_vars[$meta_key] ) ) {        $wp_query_args['meta_query'][] = array(            'key'     => $meta_key,            'value'   => esc_attr( $query_vars[$meta_key] ),            'operator' => '==',         );    }    return $wp_query_args;}然后我嘗試將它與以下內容一起使用wc_get_products():$product_list = wc_get_products( array('_author' => get_current_user_id()) );但它返回空數組。任何想法?
查看完整描述

2 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

您可以使用未記錄的參數“author”在 WC_Query 中簡單地按作者 ID 獲取產品,如下所示:


$products = wc_get_products( array(

    'status'    => 'publish',

    'limit'     => -1,

    'author'    => get_current_user_id()

) );

你將得到一個WC_Product對象數組


查看完整回答
反對 回復 2023-10-15
?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

您可以像這樣使用自定義查詢


<?php


$authorID = get_the_author_meta('ID');


$args = array(

    'post_type' => 'product',

    'post_status' => 'publish'

    'posts_per_page' => 12,

    'product_cat' => 'pants'

    'author'    => $authorID

);


$loop = new WP_Query( $args );


?>        

<div class="author_products">

    <?php if ( $loop->have_posts() ) { ?>

        <ul class="author_pubproducts">

        <?php while ( $loop->have_posts() ) : $loop->the_post();

            woocommerce_get_template_part( 'content', 'product' );

        endwhile; ?>

        </ul>

        <?php

        } else {

            echo __( 'No products found', 'textdomain' );

        }

        wp_reset_postdata();

    ?>

希望能成功


查看完整回答
反對 回復 2023-10-15
  • 2 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號