我使用下面的代码通过类似的单词获取帖子,但不起作用
$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;
哪里出问题了?
答案 0 :(得分:1)
正如我所见,您的第一个查询看起来不错。
$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);
此查询以对象数组的形式返回所有帖子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>';