如何使用自定义分类查询自定义帖子类型?

时间:2013-02-07 作者:Stephen

出于某种原因,我发现使用自定义分类法获取任何帖子都很困难。。。有人能解释我的愚蠢吗?

$args = array(
    \'post_type\' => \'adverts\',
    \'advert_tag\' => \'politics\' // Doesn\'t seem to work.
);

query_posts($args); 

while ( have_posts() ) : the_post();
    //Show Posts
endwhile;
分类声明:

add_action( \'init\', \'add_custom_taxonomy\', 0 );
function add_custom_taxonomy() {
    register_taxonomy(\'advert_tag\', \'Adverts\', 
        array(
            \'hierarchical\' => true,
            \'labels\' => array(
            \'name\' => _x( \'Advert Tags\', \'taxonomy general name\' ),
            \'singular_name\' => _x( \'Advert Tag\', \'taxonomy singular name\' ),
            \'search_items\' =>  __( \'Search Advert Tags\' ),
            \'all_items\' => __( \'All Advert Tags\' ),
            \'parent_item\' => __( \'Parent Advert Tag\' ),
            \'parent_item_colon\' => __( \'Parent Advert Tag:\' ),
            \'edit_item\' => __( \'Edit Advert Tag\' ),
            \'update_item\' => __( \'Update Advert Tag\' ),
            \'add_new_item\' => __( \'Add New Advert Tag\' ),
            \'new_item_name\' => __( \'New Advert Tag Name\' ),
            \'menu_name\' => __( \'Advert Tags\' ),
        ),
        \'rewrite\' => array(
            \'slug\' => \'advert-tags\',
            \'with_front\' => false,
            \'hierarchical\' => true
        ),
    );
}
自定义帖子类型声明:

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'Adverts\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Adverts\' ),
                \'singular_name\' => __( \'Advert\'),
                \'add_new\' => __( \'Add New\' ),
                \'add_new_item\' => __( \'Add a New Advert\' ),
                \'edit\' => __( \'Edit\' ),
                \'edit_item\' => __( \'Edit Advert\' ),
                \'new_item\' => __( \'New Advert\' ),
                \'view\' => __( \'View\' ),
                \'view_item\' => __( \'View Advert\' ),
                \'search_items\' => __( \'Search Adverts\' ),
                \'not_found\' => __( \'No Adverts found\' ),
                \'not_found_in_trash\' => __( \'No Adverts found in Trash\' ),
            ),
            \'supports\' => array(
                \'title\',
                \'thumbnail\',
            ),
            \'has_archive\' => true,
            \'menu_position\' => 10,
            \'public\' => true,
            \'rewrite\' => array( 
                \'slug\' => \'adverts\' 
            ),
            \'taxonomies\' => array(\'advert_tag\')
        )
    );
}

5 个回复
最合适的回答,由SO网友:Eugene Manuilov 整理而成

第一名don\'t use query_posts() ever, 请在此处阅读更多信息:When should you use WP_Query vs query_posts() vs get_posts()?.

你必须使用WP_Query 获取您需要的帖子。阅读documentation 为了它。在您的情况下,查询可能如下所示:

$the_query = new WP_Query( array(
    \'post_type\' => \'Adverts\',
    \'tax_query\' => array(
        array (
            \'taxonomy\' => \'advert_tag\',
            \'field\' => \'slug\',
            \'terms\' => \'politics\',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren\'t stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

SO网友:Aamer Shahzad

我使用此查询获取具有自定义分类法(faq\\U类别)的自定义帖子(faq帖子)。因为WP\\u Query args中的{taxonomy}参数自v.3.1起就被弃用,并引入了{tax\\u Query}。下面是工作正常的代码。

$query = new WP_Query( array(
    \'post_type\' => \'faqs\',          // name of post type.
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'faq_category\',   // taxonomy name
            \'field\' => \'term_id\',           // term_id, slug or name
            \'terms\' => 48,                  // term id, term slug or term name
        )
    )
) );

while ( $query->have_posts() ) : $query->the_post();
    // do stuff here....
endwhile;

/**
 * reset the orignal query
 * we should use this to reset wp_query
 */
wp_reset_query();

SO网友:Collins Mann

这是一段神奇的代码。我正在获取自定义post\\u类型“university\\u unit”的所有帖子,自定义分类法为“unit\\u type”,多个分类法术语为“directory”和“office”。我希望这能有所帮助。

<?php
$args = array(
    \'post_type\' => \'university_unit\',
    \'posts_per_page\' => -1,
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'tax_query\' => array(

        array(
            \'taxonomy\' => \'unit_type\',
            \'field\' => \'slug\',
            \'terms\' => array(\'directorate\', \'office\')
        )

    )
);

$Query = new WP_Query($args);
if($Query -> have_posts()):

    while($Query -> have_posts()):
        $Query -> the_post();
        ?>
        <div class="cm-post-list-item">
            <article>
                <div class="cm-post-head">
                    <h3 class="cm-text-blue">
                        <a href="<?php the_permalink(); ?>"><?php the_title();?></a>
                    </h3>
                </div>
                <div class="cm-post-body"><?php the_excerpt();?></div>
            </article>
        </div>
        <?php
    endwhile;

else:
    "No Administrative Offices Found. Try again later";
endif;
wp_reset_postdata();
?>

SO网友:Akashxolotl

这帮助我获得了CPT自定义分类的每个术语下列出的所有帖子

    <?php
    // Get list of all taxonomy terms  -- In simple categories title
    $args = array(
                \'taxonomy\' => \'project_category\',
                \'orderby\' => \'name\',
                \'order\'   => \'ASC\'
            );
    $cats = get_categories($args);

    // For every Terms of custom taxonomy get their posts by term_id
    foreach($cats as $cat) {
        ?>
        <a href="<?php echo get_category_link( $cat->term_id ) ?>">
            <?php echo $cat->name; ?> <br>
            <?php // echo $cat->term_id; ?> <br>
        </a>


            <?php
                // Query Arguments
                $args = array(
                    \'post_type\' => \'portfolio\', // the post type
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'project_category\', // the custom vocabulary
                            \'field\'    => \'term_id\',          // term_id, slug or name  (Define by what you want to search the below term)    
                            \'terms\'    => $cat->term_id,      // provide the term slugs
                        ),
                    ),
                );

                // The query
                $the_query = new WP_Query( $args );

                // The Loop
                if ( $the_query->have_posts() ) {
                    echo \'<h2> List of posts tagged with this tag </h2>\';

                    echo \'<ul>\';
                    $html_list_items = \'\';
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();
                        $html_list_items .= \'<li>\';
                        $html_list_items .= \'<a href="\' . get_permalink() . \'">\';
                        $html_list_items .= get_the_title();
                        $html_list_items .= \'</a>\';
                        $html_list_items .= \'</li>\';
                    }
                    echo $html_list_items;
                    echo \'</ul>\';

                } else {
                    // no posts found
                }

                wp_reset_postdata(); // reset global $post;

                ?>

    <?php } ?>

SO网友:mamunuzaman

这个答案现在不再有效,因为wordpress更改了它们的分类参数信息。请用这种方式。它会起作用的。这对我有用。“tax\\u query”替换为“tax”。希望它能起作用。

$the_query = new WP_Query( array(
    \'post_type\' => \'Adverts\',
    \'tax\' => array(
        array (
            \'taxonomy\' => \'advert_tag\',
            \'field\' => \'slug\',
            \'terms\' => \'politics\',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren\'t stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

结束

相关推荐