Support

Account

Home Forums Front-end Issues Query by numeric field and field as title Reply To: Query by numeric field and field as title

  • Your query should look more like this

    
    $args = array(
      'numberposts'  => -1,
      'post_type'    => 'artikel',
      'meta_query'  => array(
        'relation'    => 'AND',
        'date_clause' => array(
          'key'    => 'year',
          'value'    => 1900,
          'type'    => 'NUMERIC',
          'compare'  => '>'
        )
      ),
      'orderby' => array('date_clause' => 'ASC')
    );
    

    You do not need the “EXISTS” meta query since you only want posts that have a value greater than something.

    I have also added a “date_clause” for a index so that they can be ordered by this field and added the orderby.

    
    $query = new WP_query($arg);
    if ($query->have_posts()) {
      $last_post_year = 0; // year of last post shown initialized to zero
      while ($query->have_posts()) {
        the_post();
        $this_post_year = get_field('year');
        if ($this_post_year != $last_post_year) {
          // this post is in a different year than the last one
          echo $year;
        }
        $last_post_year = $this_post_year; // update last to this
        // anything else you want to this post
      }
    }