gpt4 book ai didi

php - 使用新的 wp_query 将 sql 查询转换为 wordpress 查询

转载 作者:可可西里 更新时间:2023-11-01 07:07:03 29 4
gpt4 key购买 nike

我正在尝试将 sql 查询转换为 wordpress 查询,但无法理解它是如何完成的?

我尝试转换为 new wp_query 的查询

$query = "  SELECT SQL_CALC_FOUND_ROWS distinct wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.id = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1
ON ( wp_posts.id = mt1.post_id )
WHERE 1 = 1
AND wp_posts.id NOT IN ( 0 )
AND wp_posts.post_type = 'topic'
AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'closed'
OR wp_posts.post_status = 'reported' )
AND ( wp_postmeta.meta_key = '_bbp_last_active_time')
GROUP BY wp_posts.id
ORDER BY wp_postmeta.meta_value DESC
LIMIT 0, 10
";
$topics = $wpdb->get_results($query, OBJECT);

所以我可以使用

while($topics->have_posts()) : $topics->the_post(); ?>

有点像

$args = array(); // how do I convert to this.
$topics = new WP_query($args);

最佳答案

尝试:

$wpq = new WP_Query();
$wpq->parse_query($query);
$posts = $wpq->get_posts();

或使用标准的 WP_Query 函数:

$wpq = new WP_Query($query);

关于php - 使用新的 wp_query 将 sql 查询转换为 wordpress 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28650898/

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