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() ) :
$link->the_post();
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',
) );
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() ) :
$link->the_post();
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',
) );
Just for your Info. I switched my Plugin to Advanced Custom Fields and created the Query like this. Maybe it was because of the "-" I used in my CustomField or it was the format of my Date.
$today = date('Ymd');
$args=array(
'post_type' => 'veranstaltungen',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'ende',
'compare' => '>=',
'value' => $today,
),
),
'meta_key' => 'start',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();
Thanks to everyone trying to help! :)
*-datum
fields? – s_ha_dum Commented Nov 20, 2015 at 15:20wpcf-start-datum
and notstart-datum
. The value looks like a timestamp, so you should be comparing the date to today's date as a timestamp, not a formatted date (which the database engine can't understand anyway). You should also look at themeta_query
examples to see how to properly format one. – Milo Commented Nov 20, 2015 at 16:13