How to run select query of post with category and tags for API?

I want to fetch multiple posts with categories and tags.
Actually, i’m creating API for my wordpress site, full code is like this :

 $mysql = mysqli_query($con,"SELECT p.post_title,
       p.ID,
       p.post_content,
       p.post_date,
       p.post_name as url,
       t.name as category_name
FROM wp_posts p,
     wp_terms t,
     wp_term_relationships r,
     wp_term_taxonomy tt
WHERE p.post_status='publish' AND
      tt.taxonomy = 'post_tag' AND
      p.id=r.object_id AND
      r.term_taxonomy_id=tt.term_taxonomy_id AND
      tt.term_id = t.term_id order by p.post_date desc limit ".(int)($pageNumber*$pageSize).",".(int)$pageSize."") or die ("error".mysqli_error($con));
   $count = mysqli_num_rows($mysql);

   if($count == 0)
    {
     $data["response"] = FALSE;
     $data["code"] = 400;
     $data["message"] = "No Data Found.";
    }
   else
    {
     $data["response"] = TRUE;
     $data["code"] = 200;

     $my_news['size'] = $pageSize;
     $my_news['nextPage'] = "http://demodoamin.com/api2.php"."?pageNumber=".(int)($pageNumber+1)."&pageSize=".(int)($pageSize*2)."&partner=".$partner."";

     $row_arr = array();
     while($row = mysqli_fetch_array($mysql,MYSQLI_ASSOC))
      {
       $rowArr = array();

       $rowArr["id"] = $row["ID"];
       $rowArr["language"] = "English";
       $rowArr["title"] = $row["post_title"];
       $rowArr["category"] = $row["category_name"];
       $rowArr["detailUrl"] = $row["url"];
       $rowArr["publishTime"] = $row["post_date"];
       $rowArr["description"] = $row["post_content"];

       $row_arr[] = $rowArr;
      }

     $my_news["rows"] = $row_arr;

     $data["data"] = $my_news;
    }

The mysqli-connection works. But when I run this code, I only get a blank page. How can I fix this?

Read more here: How to run select query of post with category and tags for API?

Leave a Reply

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