簡體   English   中英

如何使用WP_Query顯示自定義帖子中的選定類別帖子?

[英]How can I use WP_Query to display selected Category posts from the custom post?

我正在使用自定義帖子類型插件,我試圖在我的自定義帖子中僅循環選定的帖子。 我想循環選擇的類別。 有什么建議嗎?

這是我的代碼:

<?php
$loop=new WP_Query(array(
    'post_type'=>'custom post';
    'taxonomy '->'private';
    'sort_column' => 'post_date',
    'posts_per_page'=> -1 ,
    'order' => 'ASC')
); 
if ( $loop->have_posts() ){?>

    <?php 
    while ( $loop->have_posts() ) 
    {
        $loop->the_post();
        $meta=get_post_meta(get_the_id(),'');

?>

根據wp_query文檔

$loop=new WP_Query(array(
    'post_type' => 'custom post',
    'taxonomy' =>'private',
    'sort_column' => 'post_date',
    'posts_per_page'=> -1,
    'order' => 'ASC',
    'cat' => 19
)
); 

在wp_query中使用wordpress的稅務查詢

$args = array(
    'post_type'=>'custom post';
    'posts_per_page'=> -1 ,
    'order' => 'ASC'
    'orderby' => 'ID'

    'tax_query' => array(
        array(
            'taxonomy' => 'private',
            'field'    => 'slug',
            'terms'    => 'bob',
        ),
    ),
);
$query = new WP_Query( $args );

並用'terms' => '<your category slug>',替換'terms' => 'bob',可以從后端檢查Slug

像這樣用它:

<?php
$loop=new WP_Query(array(
    'post_type'=>'custom post';
    'posts_per_page'=> -1 ,
    'order' => 'ASC',
    'orderby' => 'ID',

    'tax_query' => array(
        array(
            'taxonomy' => 'private',
            'field'    => 'slug',
            'terms'    => 'bob'
        ),
    ),
);
); 
if ( $loop->have_posts() ){?>

    <?php 
    while ( $loop->have_posts() ) 
    {
        $loop->the_post();
        $meta=get_post_meta(get_the_id(),'');

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM