使用META_VALUE_NUM时是否可以按多个META_KEY排序?

时间:2013-01-16 作者:Dan

通读order & orderby documentation, 我不清楚是否支持基于多个meta_key 价值观

显然,使用meta_query 我可以基于多个键值对返回帖子,但我希望能够控制基于多个键值对返回这些结果的顺序meta_keys.

例如,我有多个类别的页面,每个类别都有数字排名。如果用户正在搜索属于三个不同类别之一的页面,我可以返回所有必要的帖子,并提供以下内容:

$query = array(
  \'order\' => \'DESC\',
  \'orderby\' => \'meta_value_num\',
  \'meta_query\' => array(
    \'relation\' => \'OR\',
    array( \'key\' => \'cat1\', \'type\' => \'numeric\' ),
    array( \'key\' => \'cat2\', \'type\' => \'numeric\' ),
    array( \'key\' => \'cat3\', \'type\' => \'numeric\' )
  );
);
然而,一旦这些结果被返回,我希望根据返回结果的任何类别中的最高数值对它们进行排序。换句话说,带有9 中的值cat1 与带有9 中的值cat3.

正在查看this answer 看来meta_key 甚至不需要\'orderby\' => \'meta_value_num\', 但这与meta_value, 这比meta_value_num... 任何澄清都是有益的。谢谢

2 个回复
SO网友:Pim Schaaf

您可能想查看WP 4.2中对“orderby”和“meta\\u query”的查询改进。详细信息位于https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query.

尝试命名meta\\u查询子句,然后按它们排序。

以下代码未经测试:

$query = array(
  \'order\' => \'DESC\',
  \'meta_query\' => array(
    \'relation\' => \'OR\',
    \'cat1-clause\' => array( \'key\' => \'cat1\', \'type\' => \'numeric\' ),
    \'cat2-clause\' => array( \'key\' => \'cat2\', \'type\' => \'numeric\' ),
    \'cat3-clause\' => array( \'key\' => \'cat3\', \'type\' => \'numeric\' )
  );
  \'orderby\' => array(
    \'cat1-clause\' => \'ASC\',
    \'cat2-clause\' => \'ASC\',
    \'cat3-clause\' => \'ASC\',
  ),
);

SO网友:forsvunnet

如果查看WP\\u Query的get\\u posts方法,通过输出传递给posts_request 滤器

// Debug the WP_Query sql
add_filter( \'posts_request\', function( $sql ) {
  echo "<pre>";
  var_dump( $sql );
  echo "</pre>";
  return $sql;
} );
结果应该如下所示:

string(426) "SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND (
  wp_postmeta.meta_key = \'cat1\'
  OR
  wp_postmeta.meta_key = \'cat2\'
  OR
  wp_postmeta.meta_key = \'cat3\'
) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC LIMIT 0, 10"
查询一开始可能看起来是正确的,但事实并非如此。它使用meta_value 从内部联接的第一行开始ORDER BY 条款在这种情况下\'cat1\'.要改变这种行为,我们可以使用posts_orderby 钩子更改ORDER BY 语句并使用MAX mysql函数,因此它将使用所有meta\\u值中的最高值,而不仅仅是第一行。

// Filter to alter the orderby sql
function nvdGMCWh_alter_posts_orderby( $sql ) {
  return "MAX( $wpdb->postmeta.meta_value+0 ) DESC";
} // - alter_posts_orderby
// Register the filter
add_filter( \'posts_orderby\', \'nvdGMCWh_alter_posts_orderby\' );

// The WP_Query
$res = new WP_Query;
$args = [
  \'order\' => \'DESC\',
  \'orderby\' => \'meta_value_num\',
  \'meta_query\' => [
    \'relation\' => \'OR\',
    [\'key\' => \'cat1\', \'type\' => \'numeric\'],
    [\'key\' => \'cat2\', \'type\' => \'numeric\'],
    [\'key\' => \'cat3\', \'type\' => \'numeric\'],
  ],
]; // - $args
// Execute the query and get the results
$result = $res->query( $args );
// Clean up the filter so it won\'t affect later queries
remove_filter( \'posts_orderby\', \'nvdGMCWh_alter_posts_orderby\' );
// Loop through the results
foreach ( $result as $post ) {
  // ... code here
} // - foreach

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post