Notifications
Clear all
Topic starter
Im working on wordpress database directly. im trying to get category detail of a post by id, so i query for post, and query for the category where multiple category of same post. I'm new for this PDO method. can any one help me for this:
//Get Categories
public function categories($post_id) {
$query = "SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = ?";
$stmt = $this->conn->prepare($query);
$stmt->execute([$post_id]);
$term = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$term[] = $row['term_taxonomy_id'];
}
$query = "SELECT * FROM wp_terms WHERE term_id IN (";
$comma = '';
for($i=0; $i<count($term); $i++){
$query .= $comma.':p'.$i; // :p0, :p1,
$comma = ',';
}
$query .= ')';
$stmt = $this->conn->prepare($query);
for($i=0; $i<count($term); $i++){
if(isset($term[$i])) {
$stmt->bindValue(':p'.$i, $term[$i], PDO::PARAM_INT);
}
}
$stmt->execute();
$category = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$category[] = array(
'id'=> $row['term_id'],
'slug'=> $row['slug'],
'title'=> $row['name']
);
}
return $category;
}
The error im getting is
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064
Posted : 06/02/2020 11:04 am