繁体   English   中英

排除Wordpress“最新帖子”功能中的精选帖子

[英]Exclude Featured Posts in Wordpress 'Recent Posts' Function

我可以使用wp_get_recent_posts函数中的“ exclude”数组排除特色帖子吗? 我有一个名为NS Featured Posts的插件,该插件通过wp查询中的键来提取特色帖子,即:

$custom_query = new WP_Query( 
    array(
        'post_type' => 'post', 
        'meta_key'   => '_is_ns_featured_post',
        'meta_value' => 'yes'
    ) 
);

我可以以某种方式使用它来定位和排除wp_get_recent_posts调用中的NS特色帖子,例如:

$recent_posts = wp_get_recent_posts(array(
        'numberposts' => 3,
        'exclude' => (the ns featured posts)
    ));

感谢您的见解。

因此,我现在对其进行了测试,并且无法在一个查询中获取没有特定元键的帖子。

但是:您可以将它们从另一个查询中排除,如下所示:

    $featured_posts = get_posts( [
        'meta_key' => '_is_ns_featured_post',
        'meta_value' => 'yes',
        'fields'     => 'ids',
    ] );

    query_posts( array( 'post__not_in' => $featured_posts ) );

    while ( have_posts() ) : the_post();
        $output .= '<li>'.get_the_title().'</li>';
    endwhile;

    wp_reset_query();

诸如wp_get_recent_posts()函数可以接受与WP_Query相同的所有参数。 虽然文档仅列出了一些参数,但您可以使用全套参数。

您曾建议在查询中使用exclude ,但是这将希望排除帖子的ID。 您当然可以先抢那些,但这并不是最有效的解决方案。

在单个查询中执行此操作的方法是使用元查询选项。 这些帖子被标记为一个元密钥,并且元查询将允许您排除那些。 您需要同时检查元密钥的存在和值为“是”的情况。

例:

$recent_posts = wp_get_recent_posts( array(
    'numberposts' => 3,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_is_ns_featured_post',
            'value' => 'yes',
            'compare' => '!=',
        ),
        array(
            'key' => '_is_ns_featured_post',
            'compare' => 'NOT EXISTS',
        ),
    )
) );

参考: https : //codex.wordpress.org/Class_Reference/WP_Meta_Query

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM