按关系字段(ACF)的Elementor帖子的自定义查询过滤器
我有一个名为艺术家"的自定义帖子类型设置.每个单个艺术家"都是一个页面(如果需要,可以提供艺术家资料),并具有通过高级自定义字段"(ACF)通过关系字段类型与该艺术家相关联的产品的列表.我需要将这些产品显示在艺术家页面上的类别中.因此,在Elementor中,我需要指定一个查询过滤器ID"以将产品简单地划分为类别.
I have a custom post type setup called 'Artists'. Each Single Artist is a page (artist profile if you wish), has a list of products that are associated with that artist via the relationship field type through Advanced Custom Fields (ACF). I need the products to be displayed within their categories on the artist page. So within Elementor I need to specify a 'Query Filter ID' to simply split the products into categories.
到目前为止我尝试过的事情
由于要生成查询ID,我试图通过自定义查询在列表中仅显示特定类别的产品.
I am trying to display only products from a certain category in a list via a custom query as I need to generate a Query ID.
我一直在尝试许多不同的方法来做到这一点,但现在我很茫然.我拥有的最新代码在这里...我想念什么?
I've been trying a bunch of different ways to do this but now I'm at a loss. The latest code I have is here... what am I missing?
/** Product category 'SOFTWARE' **/
add_filter( 'software_product', 'product_category_software' );
function product_category_software( $variable ) {
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'software',
),
),
);
return $variable;
}
答案
您需要使用此
https://developers.elementor.com/custom-query-filter/#Using_the_Custom_Filter
arbitrary_name_query_id
-您在Elementor字段中填写的QUERY ID,
arbitrary_name_query_id
- the QUERY ID that you fill in the Elementor field,
要放置在 functions.php
中的代码:
add_action( 'elementor/query/arbitrary_name_query_id', function( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = [
'key' => 'acf_key', // put ACF relationship field name
'value' => get_the_ID(),
'compare' => '=',
];
$query->set( 'meta_query', $meta_query );
} );
关于给定的代码
首先,如过滤器上的Wordpress文档所述:
它们(即过滤器)为函数修改其他数据提供了一种方法功能.
They (i.e. filters) provide a way for functions to modify data of other functions.
这意味着过滤器函数(在本例中为 product_category_software
)接收经过后续修改的数据并将其返回:
Which means that the filter function, in this case it's product_category_software
, receives data that subsequntly modifies and returns it back:
add_filter( 'software_product', 'product_category_software' );
function product_category_software( $variable ) { // receive $variable
/**
* Modify $variable here
*/
return $variable; // return modified $variable
}
在您的情况下, product_category_software
不会修改接收到的内容,但是会引入新的数据 $ query_args
和平度,该意义不会出现,因为它不会返回.
In your case product_category_software
doesn't modify what receives, but introduces new peace of data $query_args
which makes no sence, since it is not going to be returned.
其次,, add_filter
函数的第一个参数应该是现有的过滤器名称,而 software_product
则不是.
Secondly, the first argument of the add_filter
function should be an existent filter name, the software_product
is not.
第三,产品类别分类的正确名称是 product_cat
.
Thirdly, the correct name of the taxonomy of product categories is product_cat
.
方法#1 使用 pre_get_posts
Aproach #1 Modfying main query with pre_get_posts
function your_arbitrary_name( $query ) { // receive
if ( ! is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query() ) {
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('software'),
)
);
$query->set( 'meta_query', $tax_query ); // modify
}
return $query; // and return
}
add_action( 'pre_get_posts', 'your_arbitrary_name', 20 );
方法2 #strong>使用 wc_get_products
或 WC_Product_Query
Aproach #2 Use wc_get_products
or WC_Product_Query
例如,在您的控制器甚至模板中使用:
Example, use in your Controller or even in the template:
$args = array(
'category' => array( 'software' ),
);
$products = wc_get_products( $args );