ACF 按转发器字段的子字段过滤自定义帖子
ACF filter custom posts by subfield of repeater field
我正在关注 ACF 文档中的官方 guide,但未能正确理解。我正在使用高级自定义字段和自定义 post 类型 UI 插件。
我有一个名为 materials 的自定义 post 类型,每个 material 都有一个 files 转发器字段,子字段之一是 title。我想根据标题查询 posts 并使用 ajax.
将结果放到页面上
这是我的 functions.php:
function materialsSearchAjax() {
$html = "";
$keyword = $_POST['keyword'];
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' =>
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
);
$query = new WP_Query( $args );
$posts = array();
$html .= '<div class="Materials-students">';
while( $query->have_posts() ) : $query->the_post();
$html .= '<div class="Files-list u-padding-left--12">';
if( have_rows('files') ){
while ( have_rows('files') ) : the_row();
$html .= '<div class="Files-item u-margin-right--30 u-margin-bottom--18">';
$html .= '<div class="Files-itemImage"></div>';
$html .= '<a href="' . the_sub_field("document") . '" target="_blank" class="Files-itemLink">';
$html .= the_sub_field('title');
$html .= '</a>';
$html .= '</div>';
endwhile;
}
$html .= '</div>';
endwhile;
$html .= '</div>';
wp_reset_query();
return $html;
}
// filter
function materials_where( $where ) {
$where = str_replace("meta_key = 'files_%", "meta_key LIKE 'files_%", $where);
return $where;
}
function igs_scripts_styles() {
wp_enqueue_script( 'ajaxMaterialsSearch', get_template_directory_uri() . '/assets/scripts/ajaxMaterialsSearch.js', array(), false, true );
wp_localize_script( 'ajaxMaterialsSearch', 'ajax_data_object', array( 'url' => admin_url( 'admin-ajax.php' )) );
}
add_action('wp_ajax_nopriv_materialsSearchAjax', 'materialsSearchAjax');
add_action('wp_ajax_materialsSearchAjax', 'materialsSearchAjax');
add_filter('posts_where', 'materials_where');
add_action('wp_enqueue_scripts', 'igs_scripts_styles');
这是我的 ajax:
(function($) {
// Trigger submit
$('.Search-magnifier').on('click', function(){
var $form = $(this).parent();
$($form).submit();
});
$('.Search-form').on('submit', function(event){
event.preventDefault();
var $form = $(this);
var searchKeyword = $($form).find('input[type="search"]').val();
console.log('keyword: ' + searchKeyword);
$.ajax({
type: 'POST',
url: ajax_data_object.url,
data: {action: 'materialsSearchAjax', keyword: searchKeyword},
success: function(textStatus) {
// update the content
console.log(textStatus);
$('.Materials-students').replaceWith(textStatus);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
}
});
});
})(jQuery);
如果我在不过滤标题的情况下查询所有 material 和 post,ajax 和查询工作正常,所以唯一认为错误的是查询本身。我按照指南进行操作,但被困了几个小时。
我猜你唯一的错误是 meta_query
本身。除了(可选的)第一级 relation
,meta_query
必须是数组的数组。尝试:
$args = array(
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' => array(
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
)
);
来自WP Codex:
meta_query
(array) - Contains one or more arrays with the following keys: […]
我复制了你的案例(Ajax 除外)并且查询工作正常,所以我想这也应该适用于 Ajax 调用。
我正在关注 ACF 文档中的官方 guide,但未能正确理解。我正在使用高级自定义字段和自定义 post 类型 UI 插件。
我有一个名为 materials 的自定义 post 类型,每个 material 都有一个 files 转发器字段,子字段之一是 title。我想根据标题查询 posts 并使用 ajax.
将结果放到页面上这是我的 functions.php:
function materialsSearchAjax() {
$html = "";
$keyword = $_POST['keyword'];
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' =>
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
);
$query = new WP_Query( $args );
$posts = array();
$html .= '<div class="Materials-students">';
while( $query->have_posts() ) : $query->the_post();
$html .= '<div class="Files-list u-padding-left--12">';
if( have_rows('files') ){
while ( have_rows('files') ) : the_row();
$html .= '<div class="Files-item u-margin-right--30 u-margin-bottom--18">';
$html .= '<div class="Files-itemImage"></div>';
$html .= '<a href="' . the_sub_field("document") . '" target="_blank" class="Files-itemLink">';
$html .= the_sub_field('title');
$html .= '</a>';
$html .= '</div>';
endwhile;
}
$html .= '</div>';
endwhile;
$html .= '</div>';
wp_reset_query();
return $html;
}
// filter
function materials_where( $where ) {
$where = str_replace("meta_key = 'files_%", "meta_key LIKE 'files_%", $where);
return $where;
}
function igs_scripts_styles() {
wp_enqueue_script( 'ajaxMaterialsSearch', get_template_directory_uri() . '/assets/scripts/ajaxMaterialsSearch.js', array(), false, true );
wp_localize_script( 'ajaxMaterialsSearch', 'ajax_data_object', array( 'url' => admin_url( 'admin-ajax.php' )) );
}
add_action('wp_ajax_nopriv_materialsSearchAjax', 'materialsSearchAjax');
add_action('wp_ajax_materialsSearchAjax', 'materialsSearchAjax');
add_filter('posts_where', 'materials_where');
add_action('wp_enqueue_scripts', 'igs_scripts_styles');
这是我的 ajax:
(function($) {
// Trigger submit
$('.Search-magnifier').on('click', function(){
var $form = $(this).parent();
$($form).submit();
});
$('.Search-form').on('submit', function(event){
event.preventDefault();
var $form = $(this);
var searchKeyword = $($form).find('input[type="search"]').val();
console.log('keyword: ' + searchKeyword);
$.ajax({
type: 'POST',
url: ajax_data_object.url,
data: {action: 'materialsSearchAjax', keyword: searchKeyword},
success: function(textStatus) {
// update the content
console.log(textStatus);
$('.Materials-students').replaceWith(textStatus);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
}
});
});
})(jQuery);
如果我在不过滤标题的情况下查询所有 material 和 post,ajax 和查询工作正常,所以唯一认为错误的是查询本身。我按照指南进行操作,但被困了几个小时。
我猜你唯一的错误是 meta_query
本身。除了(可选的)第一级 relation
,meta_query
必须是数组的数组。尝试:
$args = array(
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' => array(
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
)
);
来自WP Codex:
meta_query
(array) - Contains one or more arrays with the following keys: […]
我复制了你的案例(Ajax 除外)并且查询工作正常,所以我想这也应该适用于 Ajax 调用。