我有一个 wordpress 查询,我想使用 ACF 自定义字段值搜索自定义帖子类型,但查询不起作用,我输入了正确的值,但结果为空。
WordPress 代码
function webdev_search_apple_post_data()
{
$args = array(
'post_type' => sanitize_text_field($_GET['post_type']), // custom post type
'meta_query' => array(
array(
// here I tested both "meta_key" and "key" for all of the following.
'meta_key' => 'model_no', // ACF custom field
'meta_value' => sanitize_text_field($_GET['model_no']),
'meta_compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
var_dump($query->the_post());
die();
}
}
wp_reset_postdata();
}
add_action('wp_ajax_webdev_search_laptop_post_data', 'webdev_search_apple_post_data');
add_action('wp_ajax_nopriv_webdev_search_laptop_post_data', 'webdev_search_apple_post_data');
j查询代码
$("laptop-model-input").on("keyup", function (event) {
var pattern = /^A\d{4}$/;
var enterKeyCode = 13;
var model_no = $(this).val();
let targetEl = $(this);
if (event.which === enterKeyCode && pattern.test(model_no)) {
$("#laptop-post-data-div").html('<img src="https://example.com/wp-content/uploads/2023/03/Dual-Ring-1s-200px-1.gif" style="max-width:35px;">');
$.ajax({
type: 'GET',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
'model_no': model_no,
'post_type': 'laptop_serv',
'action': 'webdev_search_laptop_post_data'
}, success: function (result) {
$("#laptop-post-data-div").html(result);
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}
});
我试过上面的代码,但它返回 NULL