按元值排序,先亲,然后自由
-
-
欢迎来到WPSE.首先,请随时访问我们的[游览]页面,以了解网站的运行方式.也请访问[帮助]以解决与网站相关的任何问题.回到您的问题,我只需要清除一些问题,您想从带有free和pro作为元值的元密钥中随机获取帖子.假设您获得10个帖子,4个专业帖子和6个免费帖子,则必须先显示4个专业帖子,然后再显示6个免费帖子.提示,请勿使用`query_posts`,而应使用`WP_Query`Welcome to WPSE. To start you off, please feel free to visit our [tour] page to get a feel on how the site operate. Also visit [help] for any site related questions. To come back to your question, I just need to clear some issues, you want to randomly fetch posts from a meta key with free and pro as meta values. Say you get 10 posts, 4 pro and 6 free, the 4 pro posts must be shown first, then the other 6 free. Just a tip, do not use `query_posts`, rather use `WP_Query`
- 0
- 2014-10-13
- Pieter Goosen
-
k表示我使用了内置的query_posts函数.tax-list.php,所以我需要创建新的cat.php文件?为新循环?或在同一文件中使用wp?k thanks means i used query_post in - inbuild function of theme. tax-list.php so i need to create new cat.php file ? for new loop ? or used wp in same file ?
- 0
- 2014-10-13
- itcgpit mark
-
使用相同的文件,只需将query_posts更改为WP_Query即可.另外,请在我的最后评论中提出建议,这是否是您要尝试做的事情Use the same file, just change `query_posts` to `WP_Query`. Also, please advice on my last comment whether this is what you are trying to do
- 0
- 2014-10-13
- Pieter Goosen
-
是的,我正在尝试. 在类别列表页面 帖子类型=列表 元值=专业且免费,但先显示专业然后免费.so按元值顺序DESC排序 WP_Query($ query_string.'&orderby=meta_value_num'); 调用未定义的函数WP_Query() 错误ya i am trying like. in page of category listing post type = listing meta value = pro and free but first display pro then free .so order by meta value order DESC WP_Query($query_string . '&orderby=meta_value_num'); Call to undefined function WP_Query() error
- 0
- 2014-10-13
- itcgpit mark
-
对于我的类别页面中的ex,如果有10个帖子,那么我想要10个,但meta值=pro首先,然后meta_value=free之后.好 . 现在随机工作.全部或仅发布或仅免费.但我想按元值排序,以便我轻松解决此问题for ex in my category page if there 10 post then i want 10 with random but meta value = pro first then meta_value = free then. ok . right now random working .with all or only post or only free. but i want order by meta value so easy for me to solve this issues
- 0
- 2014-10-13
- itcgpit mark
-
请访问[`WP_Query`](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)以了解元查询参数的工作方式以及应如何构造.另外,检查orderby参数Please visit [`WP_Query`](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters) to understand how the meta query parameters work and how it should be constructed. Also, check the orderby parameters
- 0
- 2014-10-13
- Pieter Goosen
-
2 个回答
- 投票数
-
- 2014-10-13
在我辞职之前,只有一个笔记,从不(我的重点是)利用
query_posts
创建自定义查询注意:此功能并非供插件或主题使用.如后面所述,有更好的,性能更高的选项可以更改主查询. query_posts()是通过将页面主查询替换为查询的新实例来修改页面主查询的过于简单和有问题的方法.它效率低下(重新运行SQL查询),在某些情况下(尤其是在处理帖子分页时,通常会完全失败).
而是使用
WP_Query
或get_posts
创建自定义查询,但仅无法使用pre_get_posts
修改主查询.有关更多信息,请查看这篇文章假设您的
meta_key
(请注意,只有两个值:请注意:出于测试目的,我使用了自己的meta键,您应该将其替换为自己的 ),即pro
和free
,您只需检索具有此特定meta_key
的帖子即可.您的查询将如下所示:$args = [ 'meta_key' => 'packages', 'orderby' => 'rand', 'posts_per_page' => 5, ]; $the_query = new WP_Query( $args );
但是,如果您拥有的值不止于这两个值,那么您将需要调整查询以包含
meta_value
,然后使用meta_compare
参数.调整后的查询将变为:$args = [ 'meta_key' => 'packages', 'meta_value' => 'free, pro', 'orderby' => 'rand', 'meta_compare' => 'IN', 'posts_per_page' => 5, ];
( PS!在继续之前,您应该注意
orderby
参数在版本4.0中已更改,但这在此实例中不起作用.太好了,您现在将有5条帖子是随机检索的,其
meta_value
为pro
或free
现在我们需要对它们进行排序.最好的解决方案是利用
usort
根据帖子的meta_value
的值对meta_value
中返回的帖子数组进行排序.然后,您需要取消设置$the_query->posts
,然后使用重新排序的post数组将其重置.这是完整的代码:
<?php $args = [ 'meta_key' => 'packages', 'meta_value' => 'free, pro', 'orderby' => 'rand', 'meta_compare' => 'IN', 'posts_per_page' => 5, ]; $the_query = new WP_Query( $args ); $post_metas = $the_query->posts; usort( $post_metas, function( $a, $b ){ $a = get_post_meta( $a->ID , 'packages', true ) ; $b = get_post_meta( $b->ID, 'packages', true ) ; if ( $a == $b ) { return 0; } return ( $a > $b ) ? -1 : 1; } ); unset($the_query->posts); $the_query->posts = $post_metas; if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_post_meta( get_the_ID(), 'packages', true ). '</br>' . get_the_title() . '</li>'; } echo '</ul>'; } wp_reset_postdata(); ?>
您只需要调整查询变量以适合您的需求,并调整循环以显示所需内容
尽管这不是实现此目标的唯一方法,您还可以将排序功能移至functions.php并专门针对此自定义查询
Before I fire away, just one note, NEVER (my emphasis) make use of
query_posts
to create custom queriesNote: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Rather make use of
WP_Query
orget_posts
to create custom queries, but ONLY if you can't modify the main query withpre_get_posts
. For more info, check out this postAssuming that there are only two values for your
meta_key
(PLEASE NOTE: For testing purposes, I have used my own meta key, you should replace this with your own), namelypro
andfree
, you can just retrieve posts that has this specificmeta_key
. Your query will then look something like this:$args = [ 'meta_key' => 'packages', 'orderby' => 'rand', 'posts_per_page' => 5, ]; $the_query = new WP_Query( $args );
If you however have more values than just these two, then you will need to adjust your query to include the
meta_value
and then make use of themeta_compare
parameter. Your adjusted query will then become:$args = [ 'meta_key' => 'packages', 'meta_value' => 'free, pro', 'orderby' => 'rand', 'meta_compare' => 'IN', 'posts_per_page' => 5, ];
(PS! Before I go on, you should note that the
orderby
parameter has changed in version 4.0, but this will not work in this instance.Great, you will now have 5 posts that was retrieved randomly which has either a
meta_value
ofpro
orfree
Now we need to sort them. The best possible solution is to make use of
usort
to sort the returned array of posts in$the_query->posts
according to the value of the postsmeta_value
. You will then need to unset$the_query->posts
and then reset it with the reordered post array.Here is the complete code:
<?php $args = [ 'meta_key' => 'packages', 'meta_value' => 'free, pro', 'orderby' => 'rand', 'meta_compare' => 'IN', 'posts_per_page' => 5, ]; $the_query = new WP_Query( $args ); $post_metas = $the_query->posts; usort( $post_metas, function( $a, $b ){ $a = get_post_meta( $a->ID , 'packages', true ) ; $b = get_post_meta( $b->ID, 'packages', true ) ; if ( $a == $b ) { return 0; } return ( $a > $b ) ? -1 : 1; } ); unset($the_query->posts); $the_query->posts = $post_metas; if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_post_meta( get_the_ID(), 'packages', true ). '</br>' . get_the_title() . '</li>'; } echo '</ul>'; } wp_reset_postdata(); ?>
You will just need to adjust the query variables to suit your needs and also adjust the loop to display what is needed
This is not the only way to achieve it though, you can also move your sorting function to your functions.php and specifically target this custom query
-
是的,我首先得到专业人士以及随机的感谢.但现在它的显示结果为所有帖子..但我只希望有珍珠类.就像我点击类别,我只想要该类别的帖子,谢谢 还有我的帖子类型=列表ye i got pro first as well as random thanks . but now its display result as all post.. but i want only for perticuler category. like i click on categoryand i want post of only that category thanks also my post type = listing
- 0
- 2014-10-15
- itcgpit mark
-
只需在参数中添加`'cat'=>get_queried_object('cat')-> cat_ID,`Just add `'cat' => get_queried_object('cat')->cat_ID,` to your arguments
- 0
- 2014-10-15
- Pieter Goosen
-
谢谢,我在数据库中得到了完美的输出.当我通过输出SQL.现在,我只需要更改html中表示回显的部分即可.asl但现在免费后它的专业人士,但我希望该值是随机的.?.谢谢 我还需要更改html或要在页面中显示的内容,谢谢thanks i got perfect output in database. when i pass output sql. now i just need to change in html means echo part. asl but right now its pro after free but i want that value as random .?.thanks i also need to changes in html or what i want to display in page thanks
- 0
- 2014-10-15
- itcgpit mark
-
很高兴解决了.至于HTML部分,这超出了这个问题的范围.与HTML相关的内容在该站点上也不是主题.如果您需要页面html结构的帮助,则应在stackoverflow.com上提出一个新问题,该问题更适合此类问题.谢谢Glad that is solved. As for the HTML section, that is out of scope of this question. HTML related stuff is also off topic on this site. If you need help with the html structure of your page, you should ask a new question over at stackoverflow.com which is better suited for these type of questions. Thank you
- 0
- 2014-10-15
- Pieter Goosen
-
非常感谢您保存了我的一天.我得到了正确的数据,现在只需要回显即可.图片标题和说明.与按主题甲.好的,谢谢先生:)thanks a lot u saved my day. i got right data now just need in echo. an image title and des. with as per theme formate. ok thanks sir :)
- 0
- 2014-10-15
- itcgpit mark
-
它不会占用每页的帖子..这将破坏分页...!it will not take post per page.. this will break pagination in ...!!
- 0
- 2014-10-15
- itcgpit mark
-
- 2014-10-13
您需要通过
meta_key
而不是meta_value
进行查询:$query_string . '&meta_key=name_of_key_that_stores_free_or_pro&orderby=meta_value_num'
You need to query by the
meta_key
instead ofmeta_value
:$query_string . '&meta_key=name_of_key_that_stores_free_or_pro&orderby=meta_value_num'
-
感谢您的答复,但我无法使用 query_posts($ query_string.'&meta_key=J_listing_type&orderby=meta_value_num'); 但不起作用thanks for reply but its not working i used query_posts($query_string . '&meta_key=J_listing_type&orderby=meta_value_num'); but not working
- 0
- 2014-10-13
- itcgpit mark
-
尝试`orderby=meta_value`Try just `orderby=meta_value`
- 0
- 2014-10-13
- TheDeadMedic
-
我也尝试了这个但不起作用:(i try this also but not working :(
- 0
- 2014-10-13
- itcgpit mark
我会随机获得专业列表,但我希望同时获得免费的
pro
,但首先要获得pro
列表,然后再获得meta_value
免费
列表.我还设置了
orderby
meta_value
这不起作用,并且在 cat 的查询字符串中为默认值.它需要
orderby
date
,同时具有meta value=pro
和free
.有什么建议吗?