某些帖子格式的帖子存档

时间:2014-09-13 作者:Alex

我使用以下代码对所有帖子进行自定义归档:

$taxonomy = \'category\';
$param_type = \'category__in\';
$term_args = array(
    \'orderby\' => \'slug\',
    \'order\' => \'ASC\'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
    foreach( $terms as $term ) {
        $args = array(
            "$param_type" => array($term->term_id),
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'caller_get_posts\'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
            <h3 style="margin-bottom: 0px;"><?php echo $term->name;?></h3>
            <?php
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <ul style="list-style-type: circle;">
                <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
                </ul>
            <?php 
            endwhile;
        }
    }
}
wp_reset_query();  // Restore global post data stomped by the_post().
现在,这会拉取所有帖子,但有没有办法将结果限制为某个特定的帖子格式?

与此相同:

echo $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts 
    WHERE post_type = \'post\' AND post_status = \'publish\'");

2 个回复
SO网友:Pieter Goosen

Post formats 实际上是taxonomy post_format. 我写了一篇关于什么是分类法及其层次结构的帖子,你可以查看一下here

获取分类法下所有术语的列表post_format, 简单使用get_termsvar_dump() 结果

$terms = get_terms(\'post_format\',\'hide_empty=0\');
  ?><pre><?php var_dump($terms); ?></pre><?php  
这将打印

array(5) {
  [0]=>
  object(stdClass)#207 (9) {
    ["term_id"]=>
    string(3) "142"
    ["name"]=>
    string(5) "Aside"
    ["slug"]=>
    string(17) "post-format-aside"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "142"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [1]=>
  object(stdClass)#310 (9) {
    ["term_id"]=>
    string(3) "129"
    ["name"]=>
    string(5) "Audio"
    ["slug"]=>
    string(17) "post-format-audio"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "129"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [2]=>
  object(stdClass)#309 (9) {
    ["term_id"]=>
    string(3) "105"
    ["name"]=>
    string(7) "Gallery"
    ["slug"]=>
    string(19) "post-format-gallery"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "105"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [3]=>
  object(stdClass)#308 (9) {
    ["term_id"]=>
    string(3) "128"
    ["name"]=>
    string(5) "Quote"
    ["slug"]=>
    string(17) "post-format-quote"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "128"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [4]=>
  object(stdClass)#307 (9) {
    ["term_id"]=>
    string(3) "106"
    ["name"]=>
    string(5) "Video"
    ["slug"]=>
    string(17) "post-format-video"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "106"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
}
有一件事我想强调一下,standardnot 一种令人失望的帖子格式。任何没有指定帖子格式的帖子都应该被指定一个默认的帖子格式。问题是,如果您只需要没有特殊帖子格式的帖子,那么需要运行tax_query 并排除所有具有帖子格式的帖子。

因此,要从特定的帖子格式获取帖子,可以运行tax_query 具有WP_Query

从video post格式获取所有帖子的示例:

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_format\',
            \'field\'    => \'slug\',
            \'terms\'    => \'post-format-video\',
        ),
    ),
);

$query = new WP_Query( $args );
如果您需要获取没有附加任何帖子格式的帖子,您需要执行以下操作

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_format\',
            \'field\'    => \'slug\',
            \'terms\'    => array(\'post-format-video\',\' post-format-quote\', \'post-format-gallery\', \'post-format-audio\', \'post-format-aside\' ),
            \'operator\' => \'NOT IN\',
        ),
    ),
);

$query = new WP_Query( $args );

SO网友:MSTannu

添加分类参数以指定WP\\U查询中的帖子类型。下面是一个直接来自codex的示例:

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_format\',
            \'field\'    => \'slug\',
            \'terms\'    => array( \'post-format-quote\' ),
        ),
    ),
);
$query = new WP_Query( $args );
您需要更换post-format-quote 和你需要的那个。

结束

相关推荐

如何在wp-Query中比较不同的时间戳以获取事件自定义帖子类型?

我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。但我需要一个当前时间的值来和事件开始时间进行比较。现在我从current_time(\'mysql\') 看起来是这样的:2012-02-16 13:15:31元的开始时间如下所示:201108121100如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?以下是我到目前为止的查询(值留空,时间戳回显):<?ph