我意识到这个查询是多么笨拙和低效,但SQL并不是我的强项。我使用以下查询从名为Event Posts的自定义post类型中提取最新的8个事件。然而,我还需要在这8个帖子中包含任何被粘住的帖子。由于一些非常复杂的客户端需求超出了wp\\u查询的能力,我不得不手工创建查询并使用wpdb来执行它。
有人可以帮我添加到这个查询中,也可以抓取粘贴的帖子吗?
SELECT SQL_CALC_FOUND_ROWS *, mt2.meta_value AS start_time, mt3.meta_value AS end_time, mt4.meta_value AS start_date, mt5.meta_value AS event_id, mt6.meta_value AS event_meta, mt7.meta_value AS event_thumb
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
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)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key = \'event_start_time\')
INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key = \'event_end_time\')
INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key =
\'event_start_date\')
INNER JOIN wp_postmeta AS mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = \'event_id\')
INNER JOIN wp_postmeta AS mt6 ON (wp_posts.ID = mt6.post_id AND mt6.meta_key = \'event_meta\')
INNER JOIN wp_postmeta AS mt7 ON (wp_posts.ID = mt7.post_id AND mt7.meta_key = \'event_thumbnail_url\')
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (7) )
AND wp_posts.post_type = \'espresso_event\'
AND (wp_posts.post_status = \'publish\')
AND (wp_postmeta.meta_key = \'event_start_date\'
AND (mt1.meta_key = \'event_start_date\'
AND CAST(mt1.meta_value AS DATE) >= \'" . date(\'Y-m-d\') . "\') )
GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS DATE) ASC, CAST(mt2.meta_value AS TIME) ASC
LIMIT 0, 8
最合适的回答,由SO网友:s_ha_dum 整理而成
粘性帖子保存在中的选项中的序列化数组中$wpdb->options
使用option_name
属于sticky_posts
. 因此,您需要选择该选项并添加OR
到查询。
$sticky = get_option(\'sticky_posts\');
$qry = "... OR {$wpdb->posts}.ID IN (".implode(\',\',$sticky).")";