can not run get_results() inside a page have get_results()

i have code iam trying to convert from mysql to wordpress
the code is first get posts ids from get_results then for each one
i run foreach loop and run another get_results but it does not work i tried get_row it works but only get one row and this does not help in my case

<?php 
include('wp-config.php');
global $wpdb;


            $counterd = 0;
            $counterd2 = 0;
            $counterdT = 0;
            $counterdT2 = 0;


            $query= "SELECT * FROM `wp_posts` WHERE `post_type` LIKE 'property' and post_title != 'Auto Draft' and post_status != 'trash' ORDER BY post_date DESC";
    // print_r($wpdb->query($query));
        $qu=$wpdb->get_results($query);
        foreach($qu as $rows){
                $row = get_object_vars($rows);
                $sql2="SELECT pm.meta_key,pm.meta_value,p.post_title FROM `wp_postmeta` pm LEFT JOIN `wp_posts` p ON pm.post_id = p.ID WHERE pm.post_id = ".$row['ID'];
                            // $res=$wpdb->get_row($sql2);
                                 // $meta_query = get_object_vars($res);
                                 foreach($wpdb->get_results($sql2) as $new){
                                    $counterd++;
                                 }
                                 echo $counterd;
                            // foreach($meta_query as $meta_row){
                            //  echo 'hi';
                            // }
            // print_r($row['ID']);
        }


?>

Read more here: can not run get_results() inside a page have get_results()

Leave a Reply

Your email address will not be published. Required fields are marked *