admin管理员组文章数量:1122846
I have a field called acf date for certain posts and I need to sort these posts by this field, but the value comes in this format: '20240517' and I need to sort the posts in ascending order with this field but my template has by default some queries a little bit confusing for my level
here is my code:
do_action( 'bp_before_activity_loop' ); ?>
<?php
$args_post = array (
'post_tipe' => 'post',
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'tipo-de-contenido',
'terms' => "actividades-y-talleres ",
'field' => 'slug',
'operator' => 'IN'
)
),
);
$meta_query = array();
if(isset($_COOKIE["region_filter"])){
$region_filter = $_COOKIE["region_filter"];
$sevice_range = array(
'relation' => 'OR',
array(
'key' => 'lugar',
'value' => $region_filter,
'compare' => 'LIKE'
),
array(
'key' => 'region',
'value' => $region_filter,
'compare' => 'LIKE'
)
);
array_push($meta_query, $sevice_range);
}
$args_post['meta_query'] = $meta_query;
$posts = new WP_Query( $args_post);
if(count($posts->posts)){
$args2 = array(
'secondary_id' => $posts->posts,
'action' => ['activity_update', 'new_blog_post'],
'per_page' => 8
);
}else{
$args2 = array(
'secondary_id' => array(-1),
'action' => ['activity_update', 'new_blog_post'],
'per_page' => 8
);
}
if(!empty($args)){
if(!empty($args['page'])){
$args2['page'] = $args['page'];
}
}
I have a field called acf date for certain posts and I need to sort these posts by this field, but the value comes in this format: '20240517' and I need to sort the posts in ascending order with this field but my template has by default some queries a little bit confusing for my level
here is my code:
do_action( 'bp_before_activity_loop' ); ?>
<?php
$args_post = array (
'post_tipe' => 'post',
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'tipo-de-contenido',
'terms' => "actividades-y-talleres ",
'field' => 'slug',
'operator' => 'IN'
)
),
);
$meta_query = array();
if(isset($_COOKIE["region_filter"])){
$region_filter = $_COOKIE["region_filter"];
$sevice_range = array(
'relation' => 'OR',
array(
'key' => 'lugar',
'value' => $region_filter,
'compare' => 'LIKE'
),
array(
'key' => 'region',
'value' => $region_filter,
'compare' => 'LIKE'
)
);
array_push($meta_query, $sevice_range);
}
$args_post['meta_query'] = $meta_query;
$posts = new WP_Query( $args_post);
if(count($posts->posts)){
$args2 = array(
'secondary_id' => $posts->posts,
'action' => ['activity_update', 'new_blog_post'],
'per_page' => 8
);
}else{
$args2 = array(
'secondary_id' => array(-1),
'action' => ['activity_update', 'new_blog_post'],
'per_page' => 8
);
}
if(!empty($args)){
if(!empty($args['page'])){
$args2['page'] = $args['page'];
}
}
Share
Improve this question
asked Aug 19, 2024 at 21:48
Yojaimito1Yojaimito1
111 bronze badge
1
- Juste a note: I would not recommend using $_COOKIE in your code because all WordPress sites should use caching and if you are using caching you won't be able to access those cookies properly. Unless you bypass cache when the cookie is present, but if you want a fast site, it's not recommended. If anything you should be using URL params and checking with $_GET or passing the filter data via AJAX. – WPExplorer Commented Aug 19, 2024 at 22:53
1 Answer
Reset to default 0You should be able to order your posts by adding the following to your main args:
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
Example:
$args_post = array (
'post_type' => 'post',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'tipo-de-contenido',
'terms' => 'actividades-y-talleres',
'field' => 'slug',
'operator' => 'IN'
)
),
);
You mentioned in your question that the field is named "acf date", in my code I'm assuming the field name is actually just "date" as the former wouldn't make much sense. If that's not the case be sure to change 'date' to your actual field name.
本文标签: meta querySort posts based on an acf field called fecha value return longtext 392024051739
版权声明:本文标题:meta query - Sort posts based on an acf field called fecha value return longtext '20240517' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297548a1930035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论