使用自定义字段
8 个回答
- 投票数
-
- 2011-04-24
您可以使用旧方法(我在WP 3.1.1上进行测试)为 orderby 参数定义元键...
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'some_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' ) ) ) );
You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)...
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'some_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' ) ) ) );
-
- 2016-11-15
通过使用命名查询,通常可以在WordPress 4.2中解决此问题.例如
$args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive', 'value' => 'some_value', 'type' => 'NUMERIC' // unless the field is not a number )));
对我来说,我想按数字字段排序,因此必须使用
'type' => 'NUMERIC'
.This issue in general is cleared up in WordPress 4.2 by using named queries. e.g.
$args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive', 'value' => 'some_value', 'type' => 'NUMERIC' // unless the field is not a number )));
For me, I wanted to order by a numeric field and I had to use
'type' => 'NUMERIC'
inside the meta query.-
真棒找到命名查询!Awesome find on the named queries!
- 1
- 2017-07-30
- Kaji
-
是的,命名查询也为我回答了这个问题.Yep, named queries answered the question for me, too.
- 0
- 2017-07-31
- Dalton Rooney
-
优秀的.我不确定order_clause中" value"的重要性,因为它不是必需的,因为它将按" order_in_archive"的最高值到最低值进行排序.Excellent. I'm not sure the significance of 'value' in that order_clause because it's not required as it will order it by the highest to lowest value of 'order_in_archive'.
- 1
- 2018-12-07
- Andrew Schultz
-
- 2011-04-24
解决此问题时,WP Codex实际上令人困惑.
您实际上不需要meta_query参数来使用orderby,而是使用meta_key参数,尽管已在WP Codex中弃用了meta_key参数,但该参数已在此处建立:您如何在Wordpress 3.1中将orderby与meta_query一起使用?,orderyby仍需要meta_key.
应该是
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'order_in_archive' ) )
The WP Codex is actually confusing when addressing this problem.
You do not actually need the meta_query param to use the orderby, instead it uses the meta_key param, which although by WP Codex is deprecated is has been established here: How do you use orderby with meta_query in Wordpress 3.1? that orderyby still needs the meta_key.
so it should be
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'order_in_archive' ) )
-
好的,是的,这是执行此查询的旧方法,它似乎适用于此指定的查询.无论如何,对于更复杂的查询,它将不起作用.我发现这是一个正在解决的已知问题,有关详细信息,请参见追踪票[#15031](http://core.trac.wordpress.org/ticket/15031)和[#17065](http://core.trac.wordpress.org/ticket/17065)Well, yes, this is the old method for doing this query and it appears to be working for this specified one. Anyway, for more complex queries, it won't work. I found this is a known issue that is being taken care of, details can be found in trac tickets [#15031](http://core.trac.wordpress.org/ticket/15031) and [#17065](http://core.trac.wordpress.org/ticket/17065)
- 3
- 2011-04-24
- Maor Barazany
-
- 2017-07-06
很简单:
这是我的代码:
query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) );
主要细节是:包括
meta_key
,除非包括meta_key
,否则我的代码没有排序,仅此而已:这是
directors
的director_weight
图片列表的完整代码:<?php query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) ); while (have_posts()) : the_post(); ?> <li <?php echo get_field('director_weight') ?>> <img src="<?php echo get_field('director_imagen') ?>"> </li> <?php endwhile; wp_reset_query(); ?>
It´s easy:
Here is my code:
query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) );
The main detail is: include
meta_key
, my code didn´t order unlessmeta_key
is included, and that's all:Here is the full code of a list of
directors
pictures order bydirector_weight
:<?php query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) ); while (have_posts()) : the_post(); ?> <li <?php echo get_field('director_weight') ?>> <img src="<?php echo get_field('director_imagen') ?>"> </li> <?php endwhile; wp_reset_query(); ?>
-
- 2015-08-07
请勿使用 query_posts.
$meta_query = new WP_Meta_Query( $meta_query_args ); $meta_query_args = array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'your_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' )));
Don't use query_posts.
$meta_query = new WP_Meta_Query( $meta_query_args ); $meta_query_args = array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'your_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' )));
Use the WP_Meta_Query parameters
-
- 2012-12-28
我发现这很好用.
<?php query_posts( array( 'posts_per_page' => '-1', 'post_type' => 'services', 'order' => 'DESC', 'meta_key' => '_order', 'orderby' => 'meta_value_num', //or 'meta_value_num' ) );
I found this to work quite well.
<?php query_posts( array( 'posts_per_page' => '-1', 'post_type' => 'services', 'order' => 'DESC', 'meta_key' => '_order', 'orderby' => 'meta_value_num', //or 'meta_value_num' ) );
-
问题是关于新的meta_query参数和让orderby使用它,而不是关于旧的但仍然有效的meta_key/meta_value参数.另外,我们不鼓励使用`query_posts`.The question is about the new-ish `meta_query` parameter and getting an `orderby` to work with it, and not about the older but still functional `meta_key`/`meta_value` parameters. Also, let's not encourage the use of `query_posts`.
- 3
- 2012-12-28
- s_ha_dum
-
该答案包含多种不良做法:使用-1,将其作为字符串传递,使用query_posts.应该将其删除.This answer contains multiple bad practices: using -1, passing it as a string, using query_posts. It should be removed.
- 0
- 2019-03-01
- Ihor Vorotnov
-
- 2017-01-19
我有一组自定义事件日期,我需要按日期降序进行排序.由于我的自定义事件日期存储在wp_postmeta表中,并且通常与post_date或修改日期不同,因此这对我有用:
$args = array( 'post_type' => 'events', // my post type - yours can be 'posts' 'post_status' => 'publish', // only get posts with this status 'orderby' => 'meta_value', // orderby the meta_value of the following meta_key 'meta_key' => 'my_custom_eventdate', // the custom meta_key name 'order'=> 'DESC' // sort descending ); $posts = new WP_Query($args);
然后您可以像这样循环遍历$posts:
foreach($posts->posts as $p){ $post_id = $p->ID; // and so on ... // # example of how I retrieve my event date $event = get_post_meta($post_id, 'my_custom_eventdate', true); }
I had a set of custom event dates I needed to sort by date descending. Since my custom event date was stored in my wp_postmeta table, and was typically different to the post_date or modified dates, this worked for me:
$args = array( 'post_type' => 'events', // my post type - yours can be 'posts' 'post_status' => 'publish', // only get posts with this status 'orderby' => 'meta_value', // orderby the meta_value of the following meta_key 'meta_key' => 'my_custom_eventdate', // the custom meta_key name 'order'=> 'DESC' // sort descending ); $posts = new WP_Query($args);
You can then loop through $posts like so:
foreach($posts->posts as $p){ $post_id = $p->ID; // and so on ... // # example of how I retrieve my event date $event = get_post_meta($post_id, 'my_custom_eventdate', true); }
-
- 2019-03-17
为我工作,
$args = array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive' )));
只需要提供order_clause的密钥
This work for me,
$args = array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive' )));
Only needed to provide the key for the order_clause
您知道吗,从WP3.0开始,有一些用于自定义高级查询的选项,这很棒. 截至此,不赞成使用自定义字段的某些查询参数(例如meta_key,meta_value)作为新的meta_query参数(请参见此处 )
我尝试使用新语法进行一个非常简单的查询,即按包含指定meta_key(order_in_archive)的特定post_type(服务)查询帖子-效果令人满意. 但是-我想按meta_value对查询进行排序,但没有成功.
这是我的查询-
我也尝试通过meta_value_numeric和meta_value进行orderby,但是无论如何,结果是按发布日期排序的(就像常规帖子一样). 有人知道该怎么做吗?
谢谢