[英]Sort by ACF field in the products in my Custom Post Type in Wordpress
[英]ACF/Wordpress: Sort post list by multiple values in frontend
我有以下代碼,它運行良好,並使用 ACF 插件根據分配給每個帖子的自定義字段對帖子進行排序。 這是用於此目的的代碼
function my_pre_get_posts( $query ) {
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$query->set('order', 'ASC');
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'active_inactive');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
我有一個使用 ACF 創建的自定義字段,稱為 active_inactive,每個帖子都有 2 個值,active 和 inactive。
第一個要求工作正常,所有活動帖子首先顯示在列表中,然后是非活動帖子,但是,日期沒有正確排序,並且在活動帖子之后,所有非活動帖子都從舊日期到結束日期排序最后。
所以我也需要確定日期,所以在活動帖子列表結束並且非活動帖子開始后,所有這些非活動帖子應該從最新日期到最舊日期開始。
任何幫助,將不勝感激。
嘗試類似的方法,但由於 post_date 而沒有返回任何結果
function my_pre_get_posts( $query ) {
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$meta_query = array(
'active_inactive' => array(
'key' => 'active_inactive',
'compare' => 'EXISTS'
),
'post_date' => array(
'key' => 'post_date',
'compare' => 'EXISTS'
),
);
$order_by = array(
'active_inactive' => 'ASC',
'post_date' => 'DESC'
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', $order_by );
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'my_pre_get_posts' );
您可以添加另一個 meta_key 參數,以使用 meta_query 參數在活動帖子之后按日期降序對非活動帖子進行排序。
可以使用meta_query參數指定多個排序條件,將meta_query參數設置為一個arrays的數組,每個數組包含meta_key、meta_value和order參數。
function my_pre_get_posts( $query ) {
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$query->set('order', 'ASC');
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'active_inactive');
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => 'active_inactive',
'value' => 'active',
'compare' => '='
),
array(
'key' => 'date',
'compare' => 'EXISTS',
),
array(
'key' => 'date',
'value' => date("Ymd"),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'active_inactive',
'value' => 'inactive',
'compare' => '='
),
array(
'key' => 'date',
'compare' => 'EXISTS',
),
array(
'key' => 'date',
'value' => date("Ymd"),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'date',
'order' => 'DESC',
'compare' => 'EXISTS',
'type' => 'DATE'
)
));
}
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
我在 meta_query 參數中又添加了三個 arrays: 第一個數組檢查日期字段是否存在並且小於或等於今天的日期,這將排除未來事件。 第二個數組檢查帖子的 active_inactive 值是否等於 inactive。 第三個數組按日期降序排列非活動帖子。 此外,我添加了一個設置為 OR 的關系參數,以便滿足這兩個條件。
function my_pre_get_posts($query)
{
if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post')
{
$meta_query = array(
'active_inactive' => array(
'key' => 'active_inactive',
'compare' => 'EXISTS'
),
'modified' => array(
'key' => 'modified',
'type' => 'date',
'compare' => 'EXISTS'
),
);
$order_by = array(
'active_inactive' => 'ASC',
'modified' => 'DESC'
);
$query->set('meta_query', $meta_query);
$query->set('orderby', $order_by);
}
else
{
$meta_query = array(
'active_inactive' => array(
'key' => 'active_inactive',
'compare' => 'EXISTS'
),
'modified' => array(
'key' => 'modified',
'type' => 'date',
'compare' => 'EXISTS'
),
);
$order_by = array(
'active_inactive' => 'ASC',
'modified' => 'DESC'
);
$query->set('meta_query', $meta_query);
$query->set('orderby', $order_by);
}
}
add_action('pre_get_posts', 'my_pre_get_posts');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.