我试图遍历我的自定义查询, 并根据从该查询获得的结果制作一个新数组, 但是当我运行时它返回null
新的WP_Query($ args)
尝试了很多调试, 不知道是什么原因导致了这个问题, 这是到目前为止我的想法
function getProductbySize(){
$category = $_REQUEST['category'];
$size = $_REQUEST['sized'];
$user_id = get_current_user_id();
$args = array(
'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => '-1', 'author' => $user_id, 'tax_query' => array(
array(
'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $category, 'operator' => 'AND'
), array(
'taxonomy' => 'Size', 'field' => 'term_id', 'terms' => $size, )
)
);
$products = new WP_Query($args);
// var_dump($products);
$data_array = [];
$data = [];
foreach($products as $product){
$data_array[] = the_ID();
$data_array[] = the_content();
$data_array[] = the_permalink();
$data[] = $data_array;
}
var_dump($data_array);
}
add_action('wp_ajax_getProductbySize', 'getProductbySize');
add_action('wp_ajax_nopriv_getProductbySize', 'getProductbySize');
当我转储数据数组时, 它会显示如下内容
array(44){[0] => NULL [1] => NULL [2] => NULL [3] => NULL [4] => NULL [5] => NULL [6] => NULL [7] =>空[8] =>空[9] =>空[10] =>空[11] =>空[12] =>空[13] =>空[14] =>空[15] => NULL [16] => NULL [17] => NULL [18] => NULL [19] => NULL [20] => NULL [21] => NULL [22] => NULL [23] => NULL [ 24] => NULL [25] => NULL [26] => NULL [27] => NULL [28] => NULL [29] => NULL [30] => NULL [31] => NULL [32] =>空[33] =>空[34] =>空[35] =>空[36] =>空[37] =>空[38] =>空[39] =>空[40] => NULL [41] => NULL [42] => NULL [43] => NULL}
谁能帮我找出我在做错什么事, 谢谢你
#1
<?php
function getProductbySize(){
$category = $_REQUEST['category'];
$size = $_REQUEST['sized'];
$user_id = get_current_user_id();
$data_array = []; // Get some random products.
$args = array(
'post_type' => 'product', 'post_status' => 'publish', 'author' => $user_id, 'product_cat' => $category, 'posts_per_page' => '-1', 'tax_query' => array(
array(
'taxonomy' => 'Size', 'field' => 'term_id', 'terms' => $size, )
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($data_array, array(
'title' =>get_the_title(), 'content' =>get_the_content(), 'permalink' =>get_the_permalink()
));
endwhile;
}
var_dump($data_array);
}
add_action('wp_ajax_getProductbySize', 'getProductbySize');
add_action('wp_ajax_nopriv_getProductbySize', 'getProductbySize');
?>
评论前必须登录!
注册