0
我有以下代碼從我的Wordpress數據庫中提取數據。我還想將這個類別添加到每一行信息中。如何在PHP函數中爲每個帖子ID選擇類別
global $wpdb;
$posts = $wpdb->get_results("
SELECT (SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_focuskw' AND post_id = $wpdb->posts.ID) AS focuskw,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_title' AND post_id = $wpdb->posts.ID) AS title,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_metadesc' AND post_id = $wpdb->posts.ID) AS metadesc,
$wpdb->posts.ID, $wpdb->posts.post_type FROM `$wpdb->posts`
WHERE $wpdb->posts.post_status = 'publish'
AND (SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_meta-robots-noindex' AND post_id = $wpdb->posts.ID)is null
AND ($wpdb->posts.post_type = 'page' OR $wpdb->posts.post_type = 'post')
");
header('Content-type:text/plain');
foreach($posts as $post) {
$permalink = get_permalink($post->ID);
echo "</br>{$post->ID},{$post->post_type},{$permalink},{$post->focuskw}";
}
據我瞭解,在接下來的部分,我要補充我的代碼:
global $wpdb;
$posts = $wpdb->get_results("
SELECT (SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_focuskw' AND post_id = $wpdb->posts.ID) AS focuskw,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_title' AND post_id = $wpdb->posts.ID) AS title,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_metadesc' AND post_id = $wpdb->posts.ID) AS metadesc,
因此,加入到最後一行上面,我是想添加類似:
(SELECT `wp_term_taxonomy` FROM `$wpdb->postmeta` WHERE `taxonomy` = 'category' AND post_id = $wpdb->posts.ID) AS category,
基本上,我知道我需要從MySQL中獲取數據,但我不確定如何格式化PHP代碼以及使用哪個路徑。我對'get_the_category'選項進行了一些研究,但我不確定如何將其實現到此代碼中。
我該如何讓這段代碼也返回帖子分類?任何積極的投入或方向是高度讚賞。
我認爲這個查詢允許一些改進空間:-( – Strawberry