I'm trying to accomplish the following to run queries on a WordPress database:
I've gotten as far as displaying the children, but having trouble adding grandchildren into the array. Here's the code:
$id = $_REQUEST['selected'];
$a = implode(',', array_map('intval', $id));
$hostname = 'localhost';
$username = 'victoria_wp_site';
$password = 'PASSWORD';
$db = new PDO("mysql:dbname=victoria_wp_site;host=$hostname", $username, $password);
$qry = $db->prepare("SELECT id,post_title
FROM `wp_posts`
WHERE `post_parent` IN ($a) and post_name not like '%revision%' ");
$qry->execute();
$i = 0;
while ($data = $qry->fetch()) {
$result[$i] = $data;
$i++;
}
$b = implode(',', array_column($result, 'id'));
$qry_grandchild = $db->prepare("SELECT id, post_title
FROM `wp_posts`
WHERE `post_parent` IN ($b) AND post_name not like '%revision%' ");
$qry_grandchild->execute();
while ($data = $qry_grandchild->fetch()) {
$result[$i] = $data;
$i++;
}
echo json_encode($result);
Not sure what I'm doing wrong.
The problem was with the format of the array used to select the grandchildren. I solved this by adding the implode function to the array before using it to select grandchildren.
$b = implode(',', array_column($result, 'id'));
$qry_grandchild = $db->prepare("SELECT id, post_title
FROM `wp_posts`
WHERE `post_parent` IN ($b) AND post_name not like '%revision%' ");
$qry_grandchild->execute();