Support

Account

Home Forums Front-end Issues Position 9 posts in grid based on Options page & Have timed posts work Reply To: Position 9 posts in grid based on Options page & Have timed posts work

  • Following along the lines of what I described above

    
    $top = array(); // holds values from options page fields
    $top_ids = array(); // will hold id values for query
    for ($i=1; $i<10; $i++) {
      // assumes fields are post object fields that returns Post Object
      $value =  get_field('kiemelt'.$i, 'option');
      if (!empty($value)) {
        // populate top array for this position
        $top[$i] = $value;
        $top_ids[] = $value->ID;
      }
    }
    
    // query to get most recent 9 posts excluding top posts
    $args = array(
      'post_type' => array('cikkek', 'mti'),
      'posts_per_page' => 9,
      'orderby' => 'date',
      'order' => 'DESC'
    );
    if (!empty($top_ids)) {
      $args['post__not_in'] = $top_ids;
    }
    $posts = get_posts($args);
    
    $next_post = 0;
    for ($i=1; $i<10; $i++) {
      $post = false;
      if (!empty($top[$i])) {
        $post = $top[$i];
      } elseif (!empty($posts[$next_post])) {
        // this elseif statement also takes into account
        // that query may not return 9 posts
        $post = $posts[$next_post];
        $next_post++;
      }
      if (!empty($post)) {
        setup_postdata($post);
        // show post
      }
    }
    wp_reset_postdata();