wp query - Problems with WP_Query, Loop, a condition and Posts per Page

admin2025-01-07  4

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',              
) );
Share Improve this question edited Nov 20, 2015 at 15:23 Howdy_McGee 20.8k24 gold badges91 silver badges175 bronze badges asked Nov 20, 2015 at 14:56 unhypeableunhypeable 1 7
  • 1 What is the format of your *-datum fields? – s_ha_dum Commented 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 Commented Nov 20, 2015 at 15:47
  • No. What does the data look like in the database? – s_ha_dum Commented Nov 20, 2015 at 15:48
  • Could you tell me how I can do that? – unhypeable Commented 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 Commented Nov 20, 2015 at 16:13
 |  Show 2 more comments

1 Answer 1

Reset to default 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! :)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263998a933.html

最新回复(0)