Всем привет.
Столкнулся с проблемой.
У поста есть 2 custom fields.
loop_event_date - дата, когда произойдет мероприятие.
featured - Закрепить как первое в списке.
add_action('rest_api_init', 'eventsApiPosts');
function eventsApiPostsFunc($data)
{
$term_slug = $data['term_slug'] ?? null;
$date = $data['date'];
$now = date('Y-m-d');
$current_page = $data['current_page'];
$lang = $data['lang'];
if ($date) {
if ($date === 'coming-soon') {
$meta_query = array(
array(
'key' => 'loop_event_date',
'value' => $now,
'compare' => '>',
'type' => 'DATE'
)
);
} else if ($date === 'past-events') {
$meta_query = array(
array(
'key' => 'loop_event_date',
'value' => $now,
'compare' => '<',
'type' => 'DATE'
)
);
} else {
$meta_query = array(
array(
'key' => 'loop_event_date',
'value' => $date,
'compare' => 'LIKE'
)
);
}
}
$tax_query = [];
if ($term_slug) {
$tax_query = array(
array(
'taxonomy' => 'events_category',
'field' => 'slug',
'terms' => [$term_slug],
)
);
}
$post_type = 'events';
$posts_per_page = 3;
$offset = ($current_page * $posts_per_page) - $posts_per_page;
$post_events = new WP_Query(array(
'post_type' => $post_type,
'order' => 'ASC',
'posts_per_page' => $posts_per_page,
'offset' => $offset,
'tax_query' => $tax_query,
'meta_key' => 'loop_event_date', // Meta key for filtering
'orderby' => 'meta_value', // Order by meta value
'order' => 'ASC', // Order direction, you can change this t
'meta_query' => $meta_query,
'meta_key' => 'featured', // Meta key for sorting
'orderby' => 'meta_value', // Order by meta value as numeric
'order' => 'ASC',
));
$total_posts = $post_events->found_posts;
$pages = ceil($total_posts / $posts_per_page);
$events_array = $post_events->get_posts();
$events_array = array_map(function ($event) use ($lang) {
$id = $event->ID;
$title = get_the_title($id);
$term = get_the_terms($id, 'events_category')[0];
if ($lang === 'ro') {
$location = get_field('title_ro', $term);
} else {
$location = $term->name;
}
$loop = get_field('loop', $id);
$event_date = $loop['event_date'];
$short_description = $loop['short_description'];
$image = get_the_post_thumbnail_url($id);
return [
'id' => $id,
'title' => $title,
'location' => $location,
'event_date' => $event_date,
'featured' => get_field('featured', $id),
'short_description' => $short_description,
'image' => $image,
'lang' => $lang,
'term_name' => $term->name,
'permalink' => get_the_permalink($id),
];
}, $events_array);
$count = count($events_array);
return [
'lang' => $lang,
'total' => $total_posts,
'count' => $count,
'pages' => $pages,
'current_page' => $current_page,
'events' => $events_array,
'offset' => $offset,
'current_page' => $current_page,
'posts_per_page' => $posts_per_page,
];
}
Так отоображаются 6 постов, но при добавлении
'meta_key' => 'featured', // Meta key for sorting
'orderby' => 'meta_value', // Order by meta value as numeric
'order' => 'ASC',
Только 3.