从类别中获取所有帖子ID

时间:2012-11-04 作者:user983248

问题:如果某个类别有任何帖子,我需要从该类别中获取帖子的ID数组。这将在插件选项页面上使用。

到目前为止,我已经:

$posts = get_posts(array(\'numberposts\' => 10000, \'category\' => 5));
但我正在努力解决如何生成一个数组,该数组只包含该类别中每个帖子的ID。

有什么想法吗?谢谢

2 个回复
最合适的回答,由SO网友:chrisguitarguy 整理而成

要记住的事情get_posts 是指使用WP_Query 对象内部。get_posts 资料来源:

<?php
/**
 * Retrieve list of latest posts or posts matching criteria.
 *
 * The defaults are as follows:
 *     \'numberposts\' - Default is 5. Total number of posts to retrieve.
 *     \'offset\' - Default is 0. See {@link WP_Query::query()} for more.
 *     \'category\' - What category to pull the posts from.
 *     \'orderby\' - Default is \'post_date\'. How to order the posts.
 *     \'order\' - Default is \'DESC\'. The order to retrieve the posts.
 *     \'include\' - See {@link WP_Query::query()} for more.
 *     \'exclude\' - See {@link WP_Query::query()} for more.
 *     \'meta_key\' - See {@link WP_Query::query()} for more.
 *     \'meta_value\' - See {@link WP_Query::query()} for more.
 *     \'post_type\' - Default is \'post\'. Can be \'page\', or \'attachment\' to name a few.
 *     \'post_parent\' - The parent of the post or post type.
 *     \'post_status\' - Default is \'publish\'. Post status to retrieve.
 *
 * @since 1.2.0
 * @uses $wpdb
 * @uses WP_Query::query() See for more default arguments and information.
 * @link http://codex.wordpress.org/Template_Tags/get_posts
 *
 * @param array $args Optional. Overrides defaults.
 * @return array List of posts.
 */
function get_posts($args = null) {
    $defaults = array(
        \'numberposts\' => 5, \'offset\' => 0,
        \'category\' => 0, \'orderby\' => \'post_date\',
        \'order\' => \'DESC\', \'include\' => array(),
        \'exclude\' => array(), \'meta_key\' => \'\',
        \'meta_value\' =>\'\', \'post_type\' => \'post\',
        \'suppress_filters\' => true
    );

    $r = wp_parse_args( $args, $defaults );
    if ( empty( $r[\'post_status\'] ) )
        $r[\'post_status\'] = ( \'attachment\' == $r[\'post_type\'] ) ? \'inherit\' : \'publish\';
    if ( ! empty($r[\'numberposts\']) && empty($r[\'posts_per_page\']) )
        $r[\'posts_per_page\'] = $r[\'numberposts\'];
    if ( ! empty($r[\'category\']) )
        $r[\'cat\'] = $r[\'category\'];
    if ( ! empty($r[\'include\']) ) {
        $incposts = wp_parse_id_list( $r[\'include\'] );
        $r[\'posts_per_page\'] = count($incposts);  // only the number of posts included
        $r[\'post__in\'] = $incposts;
    } elseif ( ! empty($r[\'exclude\']) )
        $r[\'post__not_in\'] = wp_parse_id_list( $r[\'exclude\'] );

    $r[\'ignore_sticky_posts\'] = true;
    $r[\'no_found_rows\'] = true;

    $get_posts = new WP_Query;
    return $get_posts->query($r);

}
当然,这意味着你可以使用WP_Query 接受。这包括与fields.

要获得一个仅包含ID的数组,需要执行以下操作:

<?php
$post_ids = get_posts(array(
    \'numberposts\'   => -1, // get all posts.
    \'tax_query\'     => array(
        array(
            \'taxonomy\'  => \'category\',
            \'field\'     => \'id\',
            \'terms\'     => 5,
        ),
    ),
    \'fields\'        => \'ids\', // Only get post IDs
));
或者您可以将其封装在函数中以获得更大的灵活性。

<?php
function wpse71471_get_post_ids($cat, $taxonomy=\'category\')
{
    return get_posts(array(
        \'numberposts\'   => -1, // get all posts.
        \'tax_query\'     => array(
            array(
                \'taxonomy\'  => $taxonomy,
                \'field\'     => \'id\',
                \'terms\'     => is_array($cat) ? $cat : array($cat),
            ),
        ),
        \'fields\'        => \'ids\', // only get post IDs.
    ));
}

SO网友:adamj

请参阅以下帖子:Get post ids from WP_Query?

您应该使用:wp_list_pluck

Example:

$ids = get_posts( array(
    \'post_type\' => \'contact\',
    \'pages_per_post\' => -1,
) );

var_dump(wp_list_pluck( $ids, \'ID\' ));

结束

相关推荐

MP3音频元数据上传集成:我可以从id3标签自动填充标题、字幕、描述吗?

我正在使用本地的Wordpress。org上传新媒体子系统非常常规地上传MP3文件。我们使用的MP3文件工作流使用一些重要的用户友好元数据填充ID3标记。如果上载新媒体子系统能够自动填充媒体库中文件的标题、标题和描述字段,那就太好了。有没有人为这个特定的目的创建了Wordpress插件?如果是,哪一个?如果没有,是否可能?关于我如何继续这样做,有什么建议吗?