组合元查询并赋予特定元查询更高的优先级

时间:2020-03-10 作者:Marv

有一个按日期排序的循环。但我想加入一个特定的元密钥,以获得更高的优先级。meta\\u键“high\\u prio”应将其移动到排序的顶部。(我希望这是有意义的)。

让我们讲一些代码:

$loop = new WP_Query( array(
      \'post_type\' => \'event\',
      \'posts_per_page\' => 3,
      \'cat\' => \'4\',
      \'orderby\' => array( \'meta_start\' => \'ASC\',\'meta_prio\' => \'DESC\'),
      \'meta_query\' => array(
        \'relation\' => \'OR\',
        \'meta_start\' => array(
          \'key\' => \'date_start\',
          \'type\' => \'numeric\'
        ),
        \'meta_prio\' => array(
          \'key\' => \'high_prio\',
          \'type\' => \'numeric\'
        )
      )
    ));
SQL现在如下所示:

SELECT SQL_CALC_FOUND_ROWS pdh50NA5_posts.ID
FROM pdh50NA5_posts
LEFT JOIN pdh50NA5_term_relationships
ON (pdh50NA5_posts.ID = pdh50NA5_term_relationships.object_id)
INNER JOIN pdh50NA5_postmeta
ON ( pdh50NA5_posts.ID = pdh50NA5_postmeta.post_id )
WHERE 1=1
AND ( pdh50NA5_term_relationships.term_taxonomy_id IN (4) )
AND ( pdh50NA5_postmeta.meta_key = \'date_start\'
OR pdh50NA5_postmeta.meta_key = \'high_prio\' )
AND pdh50NA5_posts.post_type = \'event\'
AND (pdh50NA5_posts.post_status = \'publish\'
OR pdh50NA5_posts.post_status = \'acf-disabled\'
OR pdh50NA5_posts.post_status = \'private\')
GROUP BY pdh50NA5_posts.ID
ORDER BY CAST(pdh50NA5_postmeta.meta_value AS SIGNED) ASC, CAST(pdh50NA5_postmeta.meta_value AS SIGNED) DESC
LIMIT 0, 3
我不擅长SQL。我了解基本知识,但order 角色太多了。

也许这必须以不同的方式进行。提前感谢!

2 个回复
最合适的回答,由SO网友:Marv 整理而成

问题是relation. 当然,必须是“和”

$loop = new WP_Query( array(
      \'post_type\' => \'event\',
      \'posts_per_page\' => 3,
      \'cat\' => \'4\',
      \'orderby\' => array(\'meta_prio\' => \'DESC\', \'meta_start\' => \'ASC\'),
      \'meta_query\' => array(
        \'relation\' => \'**AND**\',
        \'meta_start\' => array(
          \'key\' => \'date_start\',
          \'type\' => \'numeric\'
        ),
        \'meta_prio\' => array(
          \'key\' => \'high_prio\',
          \'type\' => \'numeric\'
        )
      )
    ));

SO网友:Sally CJ

正如我在评论中已经指出的,您所需要做的就是更改orderby 参数,以便meta_prio 是第一项,后跟meta_start. 所以你的orderby 应如下所示:

// This array forms an ORDER BY clause with each array item being a column that
// MySQL use when sorting the posts/results.
\'orderby\' => array(
    // This is the first column, which has the highest priority and sorts by the
    // meta high_prio.
    \'meta_prio\'  => \'DESC\',

    // This is the second column, which sorts by the meta date_start, after MySQL
    // sort the results by the meta high_prio.
    \'meta_start\' => \'ASC\',
)
因此,在上面的示例中,生成ORDER BY 子句看起来像ORDER BY <meta_prio column> DESC, <meta_start column> ASC MySQL将首先按照high_prio 然后对sorted 结果由date_start 元。

请记住,在orderby 必须与meta_query, 如果要按中的元查询子句对结果进行排序meta_query 大堆此外,请确保设置了正确的type 在元查询子句中;e、 g.如果值是如下日期2020-03-12 09:30, 然后将类型设置为DATETIME (即。\'type\' => \'DATETIME\'):

\'meta_query\' => array(
    \'relation\'   => \'OR\',
    \'meta_start\' => array(
        \'key\'  => \'date_start\',
        \'type\' => \'DATETIME\',
    ),
    \'meta_prio\'  => array(
        \'key\'  => \'high_prio\',
        \'type\' => \'NUMERIC\',
    )
),
// Make sure the array keys match those in the above meta_query array.
\'orderby\'    => array(
    \'meta_prio\'  => \'DESC\',
    \'meta_start\' => \'ASC\',
),

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在