首页 > 解决方案 > Woocommerce - 查询过滤器作者产品

问题描述

我有一个存档页面,其中显示与作者相关的产品。但是我使用的小部件只允许我选择“当前查询”并且只向我显示作者产品的完整列表,而无法编辑任何更多功能。

我有兴趣编辑要显示的产品数量并能够按特色产品进行过滤。

我看到它允许我从 ID 链接自定义查询过滤器,但我不明白如何构建代码以使其工作。我会给你一个例子,说明代码是如何的,但这种情况是针对帖子的。


    // Showing multiple post types in Posts Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
    // Here we set the query to fetch posts with
    // post type of 'custom-post-type1' and 'custom-post-type2'
    $query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
} );

标签: phpwordpresswoocommerceproductelementor

解决方案


如果该操作确实存在,那么过滤作者所需要做的就是在作者部分设置查询参数,可用选项有:

(取自https://developer.wordpress.org/reference/classes/wp_query/#author-parameters此处)

author (int) – use author id.
author_name (string) – use ‘user_nicename‘ – NOT name.
author__in (array) – use author id (available since version 3.7).
author__not_in (array) – use author id (available since version 3.7).

因此,您应该执行以下操作:

$query->set( 'author_name', 'my_author_nicename' );

推荐阅读