因此,我使用post对象,它允许我根据标题搜索自定义帖子类型。我要做的是通过自定义字段搜索自定义帖子类型,并返回自定义字段值。目前,我的函数中有以下代码。php文件:-
function my_post_object_query( $args, $field, $post_id ) {
// Get the search text
$the_search = $args[\'s\'];
// Remove it so ACF won\'t search the posts based on title
unset($args[\'s\']);
// Search based on custom field
$args[\'meta_key\'] = \'short_title\';
$args[\'meta_value\'] = $the_search;
$args[\'meta_compare\'] = \'LIKE\';
// return
return $args;
}
// filter for a specific field based on it\'s name
add_filter(\'acf/fields/post_object/query/name=product\', \'my_post_object_query\', 10, 3);
这允许我使用自定义字段进行搜索,但返回的值仍然是the\\u title()。如何将返回的值更改为相同的自定义字段;short\\u标题
谢谢
最合适的回答,由SO网友:FluffyKitten 整理而成
post\\u对象还有另一个筛选器:acf/fields/post_object/result
, 这使您可以自定义为“发布对象”字段中的每个选项显示的结果(文本)。
尝试以下操作:
function my_post_object_result( $title, $post, $field, $post_id ) {
/* Get whatever value you want here e.g. */
$short_title= get_field(\'short_title\', $post->ID);
/* do any other processing you might want to do before returning it... */
return $short_title;
}
add_filter(\'acf/fields/post_object/result/name=product\', \'my_post_object_result\', 10, 4);
Reference:
ACF Documentation for acf/fields/post_object/result