简体   繁体   中英

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

I am using custom post type plugin and I am trying to loop only selected post for a particular category within my custom post. I would like to loop only selected category. Any suggestion?

Here is my code :

<?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(),'');

?>

According to the wp_query docs

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

Use tax query of wordpress inside the wp_query

$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 );

and replace the 'terms' => 'bob', with 'terms' => '<your category slug>', Slug can be checked from back-end

use it like this:

<?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(),'');

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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