在WordPress中通过另一个插件的meta_value为自定义帖子类型订购
我有我在我的网站上显示的自定义帖子类型的列表。另外这些文章类型,我添加了一个插件,以我的WordPress,让我竖起了大拇指评级系统添加到每个岗位类型,所以它看起来是这样的:在WordPress中通过另一个插件的meta_value为自定义帖子类型订购
的代码看起来像这样:
<?php
/* The custom post types query */
$query = new WP_Query(array(
"post_type" => "motto",
"order" => "ASC",
));
while($query -> have_posts()) : $query -> the_post();
?>
/* Container with the ratings from the plugin + each post type */
<div id="motto-container">
<?=function_exists('thumbs_rating_getlink') ? thumbs_rating_getlink() : ''?>
<h3 class="abimottos">
<?php echo get_post_meta($post->ID, 'motto_titel', true); ?>
</h3>
</div>
我已经得到了这些自定义信息+的评级列表,当然,每个岗位都有一个单独的评价,我想那些收视率的值之后,为了我的自定义后的类型。我怎么能把它归档?
我知道评级的meta_key是_thumbs_rating_up(因为我已经用ARI Adminer插件修改了这个值),我能以某种方式使用这个meta_key在评级的meta_value之后排序自定义帖子类型吗?
我是PHP和数据库的新手。
您已经在使用WP_Query获取帖子,因此您可以指定meta_key以在$ args数组中进行排序,例如,
$query = new WP_Query(array(
'post_type' => 'motto',
'meta_key' => 'thumbs_rating_up',
'orderby' => 'thumbs_rating_up',
'order' => 'DESC'
));
请注意,您需要在这两个meta_key
和orderby
键名。我还假设你想按降序排列,以显示最高收视率。
编号:Wordpress Codex for WP_Query
此外,在meta_key一张纸条:
meta_keys用下划线前缀是私人的,并从自定义字段隐藏的,所以通常你会使用的版本没有下划线。这可能不是这种情况,因为我认为该评级无法在管理员中更改,但只需确保您需要使用的meta_key实际上是_thumbs_rating_up
而不是thumbs_rating_up
。
非常感谢!这帮助了我!我只需要改变顺序为“meta_value_num”,因为“thumbs_rating_up”将通过他们的post_id命令:) –
@NikolaiNowolodski很高兴我能帮上忙!我不知道你使用的是什么插件,所以我在回答中使用了“thumbs_rating_up”,因为我从你的问题中推断它有评分,但很高兴你能找到正确的键:) – FluffyKitten
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters – CBroe