首先按自定义分类对cpt进行排序,然后对其余部分进行排序

时间:2020-01-19 15:20:27

标签: wordpress sorting filter custom-post-type custom-taxonomy

我正在尝试显示来自自定义帖子类型空缺的3个最新帖子。 但是,是否有任何帖子的自定义分类法“ option”值等于“ featured”,则应首先显示它们。

因此,如果2x帖子被标记为“精选”,则它们将首先显示,然后仅显示最新的帖子。 下面的代码将仅显示标记为“精选”的帖子,但是如果只有1或2个标记为精选的帖子,那么我将不会获得3个帖子。

任何建议将不胜感激。 谢谢。

`$loop_args = array (
'post_type' => 'vacancies',
'posts_per_page' => 3,
'tax_query' => array(
   array(
  'taxonomy' => 'option',
  'field' => 'slug',
  'terms' => 'featured'
 )
 )
);
$custom_loop = new WP_Query( $loop_args );
while ( $custom_loop->have_posts() ) : $custom_loop->the_post();

$intro = get_field( 'vacancy_introduction' );
$area = get_field( 'vacancy_area' );

<?php the_title( '<h2>', '</h2>' );?>
<h3>
  <?php if( $area ): ?>
  <?php echo $area; ?>
  <?php endif; ?>
</h3>
<div>
  <?php if( $intro ): ?>
  <?php echo $intro; ?>
<?php endif; ?>
</div>

<a class="morex" href="<?php echo get_permalink(); ?>"><button><?php _e('Read More', 'tsum'); ?></button></a>

<?php endwhile; wp_reset_query(); ?>`

1 个答案:

答案 0 :(得分:0)

您只需在初始循环后添加即可。 (结束之后)

但是请删除您的wp_reset_query,以便您仍然可以访问计数。

<?php
$count = $custom_loop->found_posts;
if ($count < 3){
    $show = $count - 3;
    $second_loop = array (
        'post_type' => 'vacancies',
        'posts_per_page' => $show,
        'tax_query' => array(
           array(
              'taxonomy'    => 'option',
              'field'       => 'slug',
              'terms'       => 'featured',
              'operator'    => 'NOT IN',
                )
            )
        );

    $custom2 = new WP_Query($second_loop);

    while ( $custom2->have_posts() ) : $custom2->the_post();

    $intro = get_field( 'vacancy_introduction' );
    $area = get_field( 'vacancy_area' ); ?>

    <?php the_title( '<h2>', '</h2>' );?>
    <h3>
      <?php if( $area ): ?>
      <?php echo $area; ?>
      <?php endif; ?>
    </h3>
    <div>
      <?php if( $intro ): ?>
      <?php echo $intro; ?>
    <?php endif; ?>
    </div>

    <a class="morex" href="<?php echo get_permalink(); ?>"><button><?php _e('Read More', 'tsum'); ?></button></a>

    <?php endwhile; wp_reset_query();

}