getting-wp posts-by-tags类似于arg-wp-db query

3duebb1j  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(497)

我使用下面的代码通过一个类似于单词的标签获取帖子,但不起作用

$query = "
         SELECT *
         FROM   $wpdb->posts ,  $wpdb->terms , $wpdb->term_relationships
         WHERE  $wpdb->terms.name LIKE '$search_query%'
         AND    $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
         AND    $wpdb->term_relationship.object_id = $wpdb->posts.ID
         ORDER BY   $wpdb->posts.post_title";

     $search_songs = $wpdb->get_results($query);

        $new_query = new WP_Query( $search_songs );
        $search_songs_posts = $new_query->posts;

哪里不对?

eiee3dmh

eiee3dmh1#

如我所见,你的第一个问题看起来很好。

$query_byTag="
        SELECT wp_posts.ID
        FROM wp_posts, wp_term_relationships, wp_terms
        WHERE wp_posts.ID = wp_term_relationships.object_id
        AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
        AND wp_terms.name LIKE '".$search_query."%'";
    $search_songs = $wpdb->get_results($query_byTag);

此查询以对象数组的形式返回所有post id。

Array
(
    [0] => stdClass Object
        (
            [ID] => 1
        )

    [1] => stdClass Object
        (
            [ID] => 60
        )

)

现在你需要通过 $search_songs 对象数组 WP_Query($search_songs) 作为:

$new_query = new WP_Query( $search_songs );
echo '<pre>';
 print_r($search_songs);//returns all posts with query and others data
 echo '</pre>';

如果您只想获得帖子,则可以通过以下方式获得:

echo '<pre>';
     print_r($search_songs->posts);//returns all posts 
     echo '</pre>';

更多细节

相关问题