duanke1984 2013-09-03 12:49
浏览 36
已采纳

$ wpdb查询未运行

I'm working on a wpdb query and it's just not running. And for some reason the error reporting for wpdb isn't giving me any errors.

So, can anyone spot an obvious mistake in this query?

I'm attempting to get the meta_value of all _sell_media_attached_file keys in the post_meta table.

I'm first running a wp_query , getting the post ID's and then running each post ID through the wpdb query.

Here's what I'm working with:

        // run the loop 
        $loop = new WP_Query( array( 
            'post_type' => 'sell_media_item',
            'collection' => $club,
            'include_children' => false,
            'year' => $year,
            'monthnum' => $month,
            'day' => $day,
            'fields' => 'ids',
        ) );


        if ( $post_ids = $loop->get_posts() ) {

            $post_ids = implode( ',', $post_ids );

            //breaks here
            $atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post_ids AND meta_key = '_sell_media_attached_file' " );
            echo('<b>query:</b> <pre>'); var_dump($atts_ids);

But as I say, nothing get's output in the var_dump and if I add $wpdb->print_error(); I get no errors.

  • 写回答

2条回答 默认 最新

  • dpicx06888 2013-09-03 13:19
    关注

    In your piece of code i didn't see the initialization of wpdb class to access the wpdb class in your wordpress you have to define it as the global to access the functions of the class and also if $post_ids contains the multiple ids with comma separated you have to use the IN() clause instead of = operator

    global $wpdb;
    
            // run the loop 
            $loop = new WP_Query( array( 
                'post_type' => 'sell_media_item',
                'collection' => $club,
                'include_children' => false,
                'year' => $year,
                'monthnum' => $month,
                'day' => $day,
                'fields' => 'ids',
                'nopaging'=>true
            ) );
    
    
            if ( $post_ids = $loop->get_posts() ) {
    
                $post_ids = implode( ',', $post_ids );
    
                //breaks here
                $atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id IN($post_ids) AND meta_key = '_sell_media_attached_file' " );
                echo('<b>query:</b> <pre>'); var_dump($atts_ids);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大