Wordpress 查询自定义日期字段按天排序
Wordpress query custom date field ordered by day
我有一个名为 "employees" 的自定义 post 类型。在此我有一个名为 "birthday".
的自定义日期字段
现在我需要查询当月所有生日的列表,按日期字段的日期(生日)排序,而不是按年份排序。
这是我目前拥有的:
<?php
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'meta_key'=>'birthday',
'posts_per_page' => -1,
'orderby'=>'meta_value',
'order'=>'DESC',
'meta_query' => array(
array(
'key' => 'birthday',
'value' => $current_month,
'compare' => 'REGEXP',
'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}',
),
),
);
$query = new WP_Query( $args );if ( $query ->have_posts() ) : ?>
有了这个,我得到了一个列表:
- 05.03.2002 - 人 A
- 11.03.1998 - B
- 24.03.1995 - 人物 C
- 03.03.1970 - 人 D
我的目标是一个列表,按天排序,而不是按年排序,例如:
- 03.03.1970 - 人 D
- 05.03.2002 - 人 A
- 11.03.1998 - B
- 24.03.1995 - 人物 C
对我有什么提示吗?
谢谢
<?php
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'meta_key'=>'birthday',
'posts_per_page' => -1,
'orderby' => array(
'birthday' => 'DESC',
'meta_value'=> 'DESC',
),
'meta_query' => array(
array(
'key' => 'birthday',
'value' => $current_month,
'compare' => 'REGEXP',
'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}',
),
),
);
$query = new WP_Query( $args );if ( $query ->have_posts() ) : ?>
所以这是解决您的问题的方法。我认为添加额外的元字段“birthday_month
”和“birthday_day
”可能更容易 - 所以如果你在 post_save
挂钩上添加它们,你不必做任何额外的数据条目。
add_action('save_post', 'he_add_birthday_details', 10, 1);
function he_add_birthday_details($postid){
// Check if birthday is set
if (!empty(get_post_meta($postid, 'birthday', true))) {
// if it is set - get it
$birthday = get_post_meta($postid, 'birthday', true);
// parse the date into components
$month = date('m' , strtotime($birthday));
$day = date('d', strtotime($birthday));
// Save new postmeta fields
update_post_meta($postid, 'birthday_month', $month);
update_post_meta($postid, 'birthday_day', $day);
}
然后您可以使用新字段进行循环。
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'birthday_month',
'value' => $current_month
),
'date_clause' => array(
'key' => 'birthday_day',
'compare' => 'EXISTS'
)
),
'orderby' => 'date_clause',
'order' => 'ASC'
);
$posts = new WP_Query($args);
我有一个名为 "employees" 的自定义 post 类型。在此我有一个名为 "birthday".
的自定义日期字段现在我需要查询当月所有生日的列表,按日期字段的日期(生日)排序,而不是按年份排序。
这是我目前拥有的:
<?php
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'meta_key'=>'birthday',
'posts_per_page' => -1,
'orderby'=>'meta_value',
'order'=>'DESC',
'meta_query' => array(
array(
'key' => 'birthday',
'value' => $current_month,
'compare' => 'REGEXP',
'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}',
),
),
);
$query = new WP_Query( $args );if ( $query ->have_posts() ) : ?>
有了这个,我得到了一个列表:
- 05.03.2002 - 人 A
- 11.03.1998 - B
- 24.03.1995 - 人物 C
- 03.03.1970 - 人 D
我的目标是一个列表,按天排序,而不是按年排序,例如:
- 03.03.1970 - 人 D
- 05.03.2002 - 人 A
- 11.03.1998 - B
- 24.03.1995 - 人物 C
对我有什么提示吗? 谢谢
<?php
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'meta_key'=>'birthday',
'posts_per_page' => -1,
'orderby' => array(
'birthday' => 'DESC',
'meta_value'=> 'DESC',
),
'meta_query' => array(
array(
'key' => 'birthday',
'value' => $current_month,
'compare' => 'REGEXP',
'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}',
),
),
);
$query = new WP_Query( $args );if ( $query ->have_posts() ) : ?>
所以这是解决您的问题的方法。我认为添加额外的元字段“birthday_month
”和“birthday_day
”可能更容易 - 所以如果你在 post_save
挂钩上添加它们,你不必做任何额外的数据条目。
add_action('save_post', 'he_add_birthday_details', 10, 1);
function he_add_birthday_details($postid){
// Check if birthday is set
if (!empty(get_post_meta($postid, 'birthday', true))) {
// if it is set - get it
$birthday = get_post_meta($postid, 'birthday', true);
// parse the date into components
$month = date('m' , strtotime($birthday));
$day = date('d', strtotime($birthday));
// Save new postmeta fields
update_post_meta($postid, 'birthday_month', $month);
update_post_meta($postid, 'birthday_day', $day);
}
然后您可以使用新字段进行循环。
$current_month = date('m'); // get current month
$filter_month = $current_month; // show current month only
$args = array(
'post_type' => 'employees',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'birthday_month',
'value' => $current_month
),
'date_clause' => array(
'key' => 'birthday_day',
'compare' => 'EXISTS'
)
),
'orderby' => 'date_clause',
'order' => 'ASC'
);
$posts = new WP_Query($args);