Problems with WP_Query, Loop, a condition and Posts per Page


I am creating a website and I have Custom Types (Plugin: Types). These Custom Types are called ‘Veranstaltungen’ –> they are Events.

All of these Events have a starting date ( start-datum ) and an end date ( end-datum ) which are Custom Fields.

On my website homepage I want to display a 3 of these Events.

I’ve got the loop working with my sorting order and I can limit it to 3 by using this:

$loop = new WP_Query( array(                
    'post_type'        => 'veranstaltungen',    
    'posts_per_page'   => '3',
    'orderby'          => 'meta_key',
    'order'            => 'ASC',
    'key'              => 'start-datum',
) );

The Events of the Past are showing up.

The next 3 Events according to the custom field start-datum are the ones who shall appear.

I added this if-logic:

while( $link->have_posts() ) :

    if( date( 'm d,Y',strtotime( "today" ) ) <= types_render_field( 'end-datum', array( 'format' => 'm d, Y' ) ) )

This works but The Loop takes the “outdated posts” into account and screws up my posts_per_page.

If we think of today and I have 2 Events in the past I will just see 1 Event.

How can I fix this problem?

I tried to do this instead of the conditional and tried to exclude my posts in The Loop but it didn’t work. It just sorts my posts and prints everything:

$loop = new WP_Query( array(                
    'post_type'     => 'veranstaltungen',   
    'posts_per_page'=> '3',
    'orderby'       => 'meta_key',
    'order'         => 'ASC',
    'key'           => 'end-datum',
    'value'         =>  date( 'F j,Y',strtotime( "today" ) ),
    'compare'       => '>=',
    'type'          => 'DATE',              
) );
, unhypeable 7 years 2015-11-20T08:56:58-05:00 0 Answers 69 views 0

Leave an answer