skip to Main Content

To display some items from a post type, I’m using this WordPress query:

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

But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input ‘year’ to get the year of the project.

Thanks.

2

Answers


  1. You can use Date Parameters check below code.

    $posts = get_posts( array(
        'post_type' => 'realisations',
        'status'    => 'publish',
        'order'     => 'ASC',
        'date_query' => array(
            array( 'year' => 'yourinputyear' )
        )
    ) );
    
    Login or Signup to reply.
  2. You can use wp_query like Below

    $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";
        }
    

    Please try this way. Hope is useful.
    Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search