Orderby ACF 自定义字段日期不起作用
Orderby ACF custom field date don't work
我查看了另一个 post 但我什么也没找到,我已经在线 3 天了:
我想在 ASC
的顺序中显示 3 'evenements'
但是2018总是早于2017
$auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
),
)
)
);
?>
有人有想法吗?
我已经更新了您的代码。请尝试更新后的查询。
<?php $auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
),
)
));
?>
我已替换 'orderby' 值并在 meta_query 中添加了 'type' 参数。
希望对您有所帮助。
As your date format is not in Y-m-d
so you need to define your own
logic for shorting
function wh_posts_orderby($orderby, $query) {
//Only for custom orderby key
if ($query->get('orderby') != 'yyyymmdd_date_format')
return $orderby;
if (!( $order = $query->get('order') ))
$order = 'ASC';
global $wpdb;
$fieldName = $wpdb->postmeta . '.meta_value';
return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}
add_filter('posts_orderby', 'wh_posts_orderby', 10, 2);
将 ^^ 以上代码添加到您的活动主题 functions.php
文件中。
现在您可以按日期对 post 进行排序(格式 YYYYMMDD)
$auj = date('Ymd');
$args = [
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'yyyymmdd_date_format', //check this line
'order' => 'ASC',
'meta_query' => [
'relation' => 'AND', //added this
[
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
],
]
];
$queryEvent = new WP_Query($args);
if ($queryEvent->have_posts()) :
/* Start the Loop */
while ($queryEvent->have_posts()) : $queryEvent->the_post();
//you post
endwhile;
endif;
相关回答How do I query posts and use the 'orderby' attribute to order posts in loop according to date 'meta_value'?
希望对您有所帮助!
我通过 SQL 查询解决了问题:
$queryEvent = $wpdb->get_results( 'SELECT * FROM cci_posts INNER JOIN cci_postmeta ON cci_posts.id = cci_postmeta.post_id INNER JOIN cci_term_relationships ON cci_posts.id = cci_term_relationships.object_id WHERE cci_term_relationships.term_taxonomy_id = 4 AND cci_postmeta.meta_key = "date_de_fin" AND cci_postmeta.meta_value > 20170221 AND cci_posts.post_status = "publish" ORDER BY cci_postmeta.meta_value ASC LIMIT 3', OBJECT );
//$queryEvent = new WP_Query($args);
?>
<?php if ( $queryEvent ) : ?>
<?php /* Start the Loop */ ?>
<?php foreach ($queryEvent as $post) {
setup_postdata($post); ?>
我查看了另一个 post 但我什么也没找到,我已经在线 3 天了:
我想在 ASC
的顺序中显示 3 'evenements'
但是2018总是早于2017
$auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
),
)
)
);
?>
有人有想法吗?
我已经更新了您的代码。请尝试更新后的查询。
<?php $auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
),
)
));
?>
我已替换 'orderby' 值并在 meta_query 中添加了 'type' 参数。
希望对您有所帮助。
As your date format is not in
Y-m-d
so you need to define your own logic for shorting
function wh_posts_orderby($orderby, $query) {
//Only for custom orderby key
if ($query->get('orderby') != 'yyyymmdd_date_format')
return $orderby;
if (!( $order = $query->get('order') ))
$order = 'ASC';
global $wpdb;
$fieldName = $wpdb->postmeta . '.meta_value';
return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}
add_filter('posts_orderby', 'wh_posts_orderby', 10, 2);
将 ^^ 以上代码添加到您的活动主题 functions.php
文件中。
现在您可以按日期对 post 进行排序(格式 YYYYMMDD)
$auj = date('Ymd');
$args = [
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'yyyymmdd_date_format', //check this line
'order' => 'ASC',
'meta_query' => [
'relation' => 'AND', //added this
[
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
],
]
];
$queryEvent = new WP_Query($args);
if ($queryEvent->have_posts()) :
/* Start the Loop */
while ($queryEvent->have_posts()) : $queryEvent->the_post();
//you post
endwhile;
endif;
相关回答How do I query posts and use the 'orderby' attribute to order posts in loop according to date 'meta_value'?
希望对您有所帮助!
我通过 SQL 查询解决了问题:
$queryEvent = $wpdb->get_results( 'SELECT * FROM cci_posts INNER JOIN cci_postmeta ON cci_posts.id = cci_postmeta.post_id INNER JOIN cci_term_relationships ON cci_posts.id = cci_term_relationships.object_id WHERE cci_term_relationships.term_taxonomy_id = 4 AND cci_postmeta.meta_key = "date_de_fin" AND cci_postmeta.meta_value > 20170221 AND cci_posts.post_status = "publish" ORDER BY cci_postmeta.meta_value ASC LIMIT 3', OBJECT );
//$queryEvent = new WP_Query($args);
?>
<?php if ( $queryEvent ) : ?>
<?php /* Start the Loop */ ?>
<?php foreach ($queryEvent as $post) {
setup_postdata($post); ?>