Skip to main content
added 15 characters in body
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

Problem: I want to list posts of a category, displaying those with custom-field 'start-date' after today (in the future) with order ASC. When this is done, I want to display all those with start-date before today (in the past) with order DESC. The

The second list is no problem. But

But the first list only displays the single most recent post when I use order => 'ASC'. If I use order => 'DESC',order => 'DESC', all the posts display correctly. Where

Where do I fail in this code?

    $args = array(
                        'cat' => $cat_id,
                        'meta_key' => 'dato-start',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'paged' => $paged
                    );
                    
    $arrangementer = new WP_Query($args);
    if ( $arrangementer->have_posts() ) :
       while ( $arrangementer->have_posts() ) : $arrangementer->the_post();

            $today = date('Ymd');
            $unnagjort = FALSE;
            $startDato = get_post_meta(get_the_ID(), 'dato-start', TRUE);
            $sluttDato = get_post_meta(get_the_ID(), 'dato-slutt', TRUE);;


            if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;          
                
            if (!$unnagjort)    {
                get_template_part( 'template-parts/content', get_post_format() );
            }

            endwhile;
    $args = array(
                        'cat' => $cat_id,
                        'meta_key' => 'dato-start',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'paged' => $paged
                    );
                    
    $arrangementer = new WP_Query($args);
    if ( $arrangementer->have_posts() ) :
       while ( $arrangementer->have_posts() ) : $arrangementer->the_post();

            $today = date('Ymd');
            $unnagjort = FALSE;
            $startDato = get_post_meta(get_the_ID(), 'dato-start', TRUE);
            $sluttDato = get_post_meta(get_the_ID(), 'dato-slutt', TRUE);;


            if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;          
                
            if (!$unnagjort)    {
                get_template_part( 'template-parts/content', get_post_format() );
            }

            endwhile;

Then I run rewind_posts()rewind_posts() and the same query, but order DESC, and filter out those with $unnagjort == TRUE.

Problem: I want to list posts of a category, displaying those with custom-field 'start-date' after today (in the future) with order ASC. When this is done, I want to display all those with start-date before today (in the past) with order DESC. The second list is no problem. But the first list only displays the single most recent post when I use order => 'ASC'. If I use order => 'DESC', all the posts display correctly. Where do I fail in this code?

    $args = array(
                        'cat' => $cat_id,
                        'meta_key' => 'dato-start',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'paged' => $paged
                    );
                    
    $arrangementer = new WP_Query($args);
    if ( $arrangementer->have_posts() ) :
       while ( $arrangementer->have_posts() ) : $arrangementer->the_post();

            $today = date('Ymd');
            $unnagjort = FALSE;
            $startDato = get_post_meta(get_the_ID(), 'dato-start', TRUE);
            $sluttDato = get_post_meta(get_the_ID(), 'dato-slutt', TRUE);;


            if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;          
                
            if (!$unnagjort)    {
                get_template_part( 'template-parts/content', get_post_format() );
            }

            endwhile;

Then I run rewind_posts() and the same query, but order DESC, and filter out those with $unnagjort == TRUE.

Problem: I want to list posts of a category, displaying those with custom-field 'start-date' after today (in the future) with order ASC. When this is done, I want to display all those with start-date before today (in the past) with order DESC.

The second list is no problem.

But the first list only displays the single most recent post when I use order => 'ASC'. If I use order => 'DESC', all the posts display correctly.

Where do I fail in this code?

    $args = array(
                        'cat' => $cat_id,
                        'meta_key' => 'dato-start',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'paged' => $paged
                    );
                    
    $arrangementer = new WP_Query($args);
    if ( $arrangementer->have_posts() ) :
       while ( $arrangementer->have_posts() ) : $arrangementer->the_post();

            $today = date('Ymd');
            $unnagjort = FALSE;
            $startDato = get_post_meta(get_the_ID(), 'dato-start', TRUE);
            $sluttDato = get_post_meta(get_the_ID(), 'dato-slutt', TRUE);;


            if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;          
                
            if (!$unnagjort)    {
                get_template_part( 'template-parts/content', get_post_format() );
            }

            endwhile;

Then I run rewind_posts() and the same query, but order DESC, and filter out those with $unnagjort == TRUE.

Source Link
hal
  • 35
  • 1
  • 1
  • 9

Why does wp_query only display the most recent post when using order ASC?

Problem: I want to list posts of a category, displaying those with custom-field 'start-date' after today (in the future) with order ASC. When this is done, I want to display all those with start-date before today (in the past) with order DESC. The second list is no problem. But the first list only displays the single most recent post when I use order => 'ASC'. If I use order => 'DESC', all the posts display correctly. Where do I fail in this code?

    $args = array(
                        'cat' => $cat_id,
                        'meta_key' => 'dato-start',
                        'orderby' => 'meta_value_num',
                        'order' => 'ASC',
                        'paged' => $paged
                    );
                    
    $arrangementer = new WP_Query($args);
    if ( $arrangementer->have_posts() ) :
       while ( $arrangementer->have_posts() ) : $arrangementer->the_post();

            $today = date('Ymd');
            $unnagjort = FALSE;
            $startDato = get_post_meta(get_the_ID(), 'dato-start', TRUE);
            $sluttDato = get_post_meta(get_the_ID(), 'dato-slutt', TRUE);;


            if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;          
                
            if (!$unnagjort)    {
                get_template_part( 'template-parts/content', get_post_format() );
            }

            endwhile;

Then I run rewind_posts() and the same query, but order DESC, and filter out those with $unnagjort == TRUE.

The first query returns only the most recent requested post, the second returns all the requested posts.