Mysql 选择WordPress查询 - 显示所有带有类别,标签的帖子信息,并将其存储在另一个自定义数据库中


Mysql Select WordPress Query – Display All posts information with category, tags and store them in another custom DB

我有一个与WordPress相关的查询,它在HeidiSql中作为查询完美运行,但我想使用 PHP 将类别和标签保存在另一个自定义数据库中(此外,我可以将除类别和标签以外的其他字段保存到自定义数据库中)。

查询:

SELECT DISTINCT   
wp_posts.ID as ID, wp_posts.post_author as post_author, wp_posts.post_date as post_date, wp_posts.post_content as post_content, wp_posts.post_title as post_title, wp_posts.post_status as post_status, wp_posts.comment_status as comment_status, wp_posts.post_name as post_name, wp_posts.guid as guid, wp_posts.post_type as post_type, wp_posts.comment_count as comment_count,    
    (SELECT GROUP_CONCAT(wp_terms.name) from wp_term_relationships
        LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
        LEFT JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
    WHERE wp_posts.ID = wp_term_relationships.object_id AND wp_term_taxonomy.taxonomy = 'category') as post_category,
    (SELECT GROUP_CONCAT(wp_terms.name) from wp_term_relationships
        LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
        LEFT JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id)
    WHERE wp_posts.ID = wp_term_relationships.object_id AND wp_term_taxonomy.taxonomy = 'tags') as post_tags
FROM wp_posts
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post'

使用这个

<?php
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        array(
            'taxonomy' => 'tags',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
    ),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
    $query->the_post();
    echo '<li>' . get_the_title() . '</li>';
}
?>