我有以下查询,显示 1 篇具有特色标签的帖子,效果很好,但如果我想保留另一个特色,我必须停用第一个,我该怎么做才能只添加最后一个特色,而不停用之前的? (它们通过检查字段激活)
<?php
global $post;
$array_terms = [];
$term_destacado = 'destacado';
$data = [
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $term_destacado,
];
array_push( $array_terms, $data );
$myposts = get_posts( [
'posts_per_page' => 1,
'offset' => 0,
'post_type' => 'podcast',
'tax_query' => ['relation' => 'AND', $array_terms],
] );
if ( $myposts ) {
foreach ( $myposts as $post ) :
setup_postdata( $post ); ?>
您可以将查询更改为按修改日期排序,因为您想要精选的新帖子将是最新/最近保存的帖子。
您可以在您的查询中添加:
'order' => 'modified'
。
将其添加到
$my_posts
数组中:
$myposts = get_posts( [
'posts_per_page' => 1,
'offset' => 0,
'post_type' => 'podcast',
/* ADDED HERE */
'order' => 'modified',
'tax_query' => ['relation' => 'AND', $array_terms],
] );