在functions.php文件中调用它时,Wordpress没有从数据库获取值

时间:2017-12-19 07:33:38

标签: database wordpress wordpress-theming

我正在尝试遍历我的自定义查询并根据从该查询获得的结果创建一个新数组,但是当我运行时它返回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] =>     空值     [1] =>     空值     [2] =>     空值     [3] =>     空值     [4] =>     空值     [5] =>     空值     [6] =>     空值     [7] =>     空值     [8] =>     空值     [9] =>     空值     [10] =>     空值     [11] =>     空值     [12] =>     空值     [13] =>     空值     [14] =>     空值     [15] =>     空值     [16] =>     空值     [17] =>     空值     [18] =>     空值     [19] =>     空值     [20] =>     空值     [21] =>     空值     [22] =>     空值     [23] =>     空值     [24] =>     空值     [25] =>     空值     [26] =>     空值     [27] =>     空值     [28] =>     空值     [29] =>     空值     [30] =>     空值     [31] =>     空值     [32] =>     空值     [33] =>     空值     [34] =>     空值     [35] =>     空值     [36] =>     空值     [37] =>     空值     [38] =>     空值     [39] =>     空值     [40] =>     空值     [41] =>     空值     [42] =>     空值     [43] =>     空值   }

任何人都可以帮我找出我做错了什么事先谢谢你

1 个答案:

答案 0 :(得分:0)

    <?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');
  ?>