我在get\\u帖子中有一个元查询,它需要花很长时间才能完成。它工作得很好,但只是时间太长了。
我有一个名为event
. 在每个event
发布后,有自定义元数据:
post\\U sort\\U日期(事件日期YmdHis
格式,用于排序)我需要做的是获取下一个事件,该事件相对于$year
和$month
变量。所以如果$year = 2021
和$month = 10
(2021 10月)然后应该在2021 11月或之后找到第一个事件。
我下面的查询很好,但很慢。执行大约需要40秒,我不知道为什么。
$next_event = get_posts( array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'numberposts\' => 1,
\'fields\' => \'ids\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'relation\' => \'AND\',
array(
\'key\' => \'_post_date_year\',
\'value\' => $year,
\'compare\' => \'=\',
\'type\' => \'numeric\',
),
array(
\'key\' => \'_post_date_month\',
\'value\' => $month,
\'compare\' => \'>\',
\'type\' => \'numeric\',
),
),
array(
\'key\' => \'_post_date_year\',
\'value\' => $year,
\'compare\' => \'>\',
\'type\' => \'numeric\',
),
),
\'meta_key\' => \'_post_sort_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
) );
我猜我的查询结构不好,这就是为什么它这么慢,但我不知道如何加快它。
UPDATE: Sorry, I forgot to add that there are only about 10 event posts. Also, I think I actually just fixed it by reversing the order of the OR conditions. After doing that it runs at a perfectly normal (basically instant) speed. If anyone know why that fixed it, I would love to know.
SO网友:Gavin
所以,我实际上通过颠倒OR条件的顺序解决了这个问题。如果有人知道这是为什么,我很想知道。
$next_event = get_posts( array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'numberposts\' => 1,
\'fields\' => \'ids\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'_post_date_year\',
\'value\' => $year,
\'compare\' => \'>\',
\'type\' => \'numeric\',
),
array(
\'relation\' => \'AND\',
array(
\'key\' => \'_post_date_year\',
\'value\' => $year,
\'compare\' => \'=\',
\'type\' => \'numeric\',
),
array(
\'key\' => \'_post_date_month\',
\'value\' => $month,
\'compare\' => \'>\',
\'type\' => \'numeric\',
),
),
),
\'meta_key\' => \'_post_sort_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
) );