posts , $wpdb->terms-6ren">
gpt4 book ai didi

mysql - 通过类似于 arg 的标签获取 wp 帖子 - wp db 查询

转载 作者:行者123 更新时间:2023-11-29 07:26:35 29 4
gpt4 key购买 nike

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

$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;

哪里错了?

最佳答案

正如我所见,您的第一个查询看起来不错。

$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>';

For more details

关于mysql - 通过类似于 arg 的标签获取 wp 帖子 - wp db 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53370014/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com