0

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',              
) );
7
  • 1
    What is the format of your *-datum fields?
    – s_ha_dum
    Nov 20, 2015 at 15:20
  • Do you mean how I display these things in the Template or in the Backend? I use them in the Templat like this: <?php echo types_render_field("start-datum", array('format'=>'m d, Y'));?>
    – unhypeable
    Nov 20, 2015 at 15:47
  • No. What does the data look like in the database?
    – s_ha_dum
    Nov 20, 2015 at 15:48
  • Could you tell me how I can do that?
    – unhypeable
    Nov 20, 2015 at 15:59
  • 1
    I don't know how that plugin works, it would seem you need to be using the key wpcf-start-datum and not start-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 the meta_query examples to see how to properly format one.
    – Milo
    Nov 20, 2015 at 16:13

1 Answer 1

0

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! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.