admin 管理员组文章数量: 1184232
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
|
Show 2 more comments
1 Answer
Reset to default 0Just 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! :)
本文标签: wp queryProblems with WPQuery Loop a condition and Posts per Page
版权声明:本文标题:wp query - Problems with WP_Query, Loop, a condition and Posts per Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1736263998a1728626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
*-datumfields? – s_ha_dum Commented Nov 20, 2015 at 15:20wpcf-start-datumand 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_queryexamples to see how to properly format one. – Milo Commented Nov 20, 2015 at 16:13