我需要这样做:
$postsOrder = get_sub_field(\'posts-ordering\');
if ($postsOrder = \'post_views_count\') {
$queryPopular = array (
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
);
}
$query = new WP_Query(
array(
\'posts_per_page\' => $postsCount,
\'post_type\' => $postsType->name,
\'order\' => \'DESC\',
\'orderby\' => $postsOrder,
\'taxonomy\' => $postsTaxonomy,
$queryPopular
),
);
关键是,如果$postsOrder等于“post\\u views\\u count”,那么在$query中应该添加另外两个参数。如何正确操作?
SO网友:Chetan Vaghela
您可以使用以下代码添加其他两个参数作为条件。
$args = array(
\'posts_per_page\' => $postsCount,
\'post_type\' => $postsType->name,
\'order\' => \'DESC\',
\'orderby\' => $postsOrder,
\'taxonomy\' => $postsTaxonomy,
);
$postsOrder = get_sub_field(\'posts-ordering\');
if ($postsOrder == \'post_views_count\') {
$args[\'meta_key\'] = \'post_views_count\';
$args[\'orderby\'] = \'meta_value_num\';
}
$query = new WP_Query($args);