按标签(优先级)然后按类别(回填)的 Wordpress 查询

Wordpress Query by Tag(Priority) then Category(backfill)

我到处搜索,但一直找不到解决我的问题的办法。希望这不是我无法通过此处和 google.

上的搜索找到的重复问题

我正在尝试 wp_query return 一组结果 (10),这些结果由在当前页面类别中找到的任何帖子填充。我能够通过...

$postCategories = get_the_category();

$atts = array ( 
    'posts_per_page' => 10,
    'tag' => 'sticky',
    'category_name' => $postCategories[0]->slug,
);

但我遇到问题的地方是标签。我希望所有带有标签 'sticky' 的帖子优先于类别匹配引入的任何帖子,同时添加的结果仍不超过 10 个。

任何帮助或指导将不胜感激,因为我是 php 的新手。谢谢

我认为这对你有用,但如果不了解你的项目的具体细节,就很难确定。

<ul>
    <?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
    <?php $args_sticky = array(
        'post__in'  => $sticky,
        'posts_per_page' => 10, // Limit to 10 posts
        'ignore_sticky_posts' => 1
    ); ?>
    <?php $sticky_query = new WP_Query( $args_sticky ); ?>
    <?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
    <?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
    <?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
        <?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
            <li><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
    <?php endif; ?>

    <?php wp_reset_query();  // Restore global post data ?>

    <?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
        <?php $postCategories = get_the_category(); ?>
        <?php $loop = new WP_Query( array( 'post_type' => 'post', 
            'posts_per_page' => $remaining_posts,
            'post__not_in' => get_option( 'sticky_posts' ),
            'category_name' => $postCategories[0]->slug
        ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
        <?php wp_reset_query();  // Restore global post data ?>
    <?php endif; ?>
</ul>