Wordpress通过自定义分类法获得自定义帖子

问题描述 投票:1回答:1

我创建了一个自定义帖子和自定义分类

我需要使用分类法搜索帖子

使用此代码但结果为null

$properties = new WP_Query( 
    array( 'posts_per_page' => 10, 
            'orderby' => 'ID',
            'order' => 'DESC',
            'post_type' => 'real-property',
            'post_status' => 'publish',
            'tax_query' => array(
                        array(
                            'taxonomy' => 'tenure',
                            'terms' => 'val',
                            'field' => 'slug'
                            )
                        ),                                                  
            ) 

        );
wordpress
1个回答
0
投票

我自己找到了一个解决方案:问题是在数组中传递值的WP_Query()函数

<?php 
$args = array( 'posts_per_page' => 10, 
    'orderby' => 'ID',
    'order' => 'DESC',
    'post_type' => 'real-property',
    'post_status' => 'publish',
    'tax_query' => array(
                array(
                    'taxonomy' => 'tenure',
                    'terms' => 'val',
                    'field' => 'slug'
                    )
                ),                                                  
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :

   while ( $the_query->have_posts() ) : $the_query->the_post(); 

      the_title();

   endwhile;

      wp_reset_postdata();

else :
   esc_html_e( 'Sorry, no posts matched.' );
endif; 
?>
© www.soinside.com 2019 - 2024. All rights reserved.