首页 > 解决方案 > 过滤 WordPress 中的查询

问题描述

为了显示帖子类型中的一些项目,我使用了这个 WordPress 查询:

$posts = get_posts(array(
    'post_type' => 'realisations',
    'status'    => 'publish',
    'order'     => 'ASC'
));

但是如何根据帖子类型页面中的信息过滤此查询返回的数据?例如,我有一个输入“年份”来获取项目的年份。

谢谢。

标签: wordpress

解决方案


您可以像下面一样使用 wp_query

$args = array (
    'post_type'              => array( 'realisations' ),
    'post_status'            => array( 'publish' ),
    'order'                  => 'ASC',
    'orderby'                => 'date',
    'year'                   => 'yourinputyear' // 2021
   );
    $query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
        <?php while ( $query->have_posts() ) : $query->the_post();
                   echo get_the_title();
              endwhile;
}else{
        echo "Data not found";
    }

请尝试这种方式。希望有用。谢谢


推荐阅读