How can I use WP_Query to display selected category entries from a custom post?

I am using a custom post type plugin and I am trying to create only the selected entry for a specific category in my custom mail. I would like to create only the 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(),'');

?>
+4
source share
3 answers

According to 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
)
); 
0
source

Use wordpress tax query inside 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 'terms' => 'bob',with 'terms' => '<your category slug>',. Slug can be checked from the back-end

0
source

:

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

?>
0

Source: https://habr.com/ru/post/1628824/


All Articles