php – How to get the custom post data using post id in WordPress?

Question

I am using WordPress, I have to get the custom post type data using post id and I tried the below code but I am getting the error

WordPress database error: [You have an error in your SQL syntax; check
the manual that corresponds to your MariaDB server version for the
right syntax to use near ‘* FROM WHERE 1=1 AND .ID = 972 AND
.post_type=”test” AND ((.post_s…’ at line 1]

I am getting query and array output below.

 Array ( [p] => 972 [post_type] => test [post_status] => publish [order] => DESC ) SELECT .* FROM WHERE 1=1 AND .ID = 972 AND .post_type="test" AND ((.post_status="publish")) ORDER BY .post_date DESC 

I am using the below code

 function postData($wpdb, $parentsdata){
        $args = array(
                      'p' => $parentsdata['utm_id'],
                      'post_type' => 'test',    
                      'post_status' => 'publish',
                      'order'      => 'DESC'
    );
    
      $related = new WP_Query($args);
      print_r($args); // checking parameters
      echo $wpdb->last_query; // query
         
         //$my_post = get_post($parentsdata['utm_id']);
        //echo $my_post->post_title;
    
         wp_reset_query();
     }

0
Naren Verma 1 year 2022-01-07T22:20:00-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse