For one of my (wordpress) projects I have an event page that shows all upcoming events. The problem is, I need to show only 2 'future' events.
I now have: 5 events in total, 3 in the past (shouldn't display anymore) and 2 future events (should display). When I use the showposts => 2 no future posts are being displayed, when I do showposts => 5, the 2 future events do display.
How can I fix that only 2 future events will be displayed?
<?php
$months = array("", "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december");
query_posts( array( 'post_type' => 'events', 'showposts' => 5) );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$dateparts = explode("-",get_post_meta(get_the_ID(), 'date')[0] );
$date = get_post_meta(get_the_ID(), 'date')[0];
$event_date = \DateTime::createFromFormat('d-m-Y', $date);
$now = new DateTime() ;
$past_event = ($event_date <= $now);
if(!$past_event){
?>
Thanks in advance
Create a secondary loop and meta query on 'date' fields.I hope this will helpful.
<?php
$event_query = new WP_Query(
array(
'post_type' => 'event',
'meta_key' => 'date',
'order_by' => 'date',
'order' => 'asc',
'meta_query' => array(
array(
'key' => 'date',
'value' => date("n"),
'compare' => '>=',
)
)
)
);
?>
<?php while($event_query->have_posts()): $event_query->the_post(); ?>
Related
I have my checkbox custom field (show_days) as values (1-7) to represent (Moo-Sun) and I tested it with an echo and it is returning the correct value. Example today is Tuesday and the echo is showing 2.
I am trying to compare it to the current date strftime("%u", time()); to only show posts that have the current day check marked. Its not working and Im wondering if I need to add an in_array somewhere. Thanks for your help.
<?php
$days = get_field('show_days');
$date = strftime("%u", time());
if ($query->have_posts() && $days = $date ) { while( $query->have_posts() ) {
$query->the_post();
echo '<div class="onAir"><h3>Currently On Air: ';
the_title();
echo $days. '</h3></div>';
} wp_reset_postdata();
}
?>
You need to add the comparison ($days = $date) as a meta_query to the WP_Query arguments.
<?php
$datetime = date('Y-m-d');
$day_number = date('w', strtotime($datetime));
$args = array(
'post_type' => 'your_post_type',
'meta_key' => 'show_days',
'meta_value' => $day_number,
'meta_compare' => '=',
);
$query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="onAir">
<h3>Currently On Air: <?php the_title(); ?> <?php echo $day_number; ?></h3>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Alrighty...after a lot of different comparisons and pulling out of my hair, I got it sorted it out. Comparing two custom field time ranges with the current time, and then comparing the selected day of the week with the current day and WP Query the posts and on top of that....my Wordpress timestamp was 6 hours ahead. Ugh! haha, This code did it
<?php
$time = current_time('H:i:s');
date_default_timezone_set('America/Denver');
$date = strftime("%A", time());
$shows = get_field('station_shows', false, false);
$query = new WP_Query ( array(
'post_type' => 'shows',
'posts_per_page' => 1,
'post__in' => $shows,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_time',
'value' => $time,
'compare' => '<=',
'type' => 'TIME',
),
array(
'value' => $time,
'key' => 'end_time',
'compare' => '>=',
'type' => 'TIME',
),
'relation' => 'AND',
array(
'key' => 'show_days',
'value' => $date,
'compare' => 'LIKE',
)
)));
if ($query->have_posts() ) { while( $query->have_posts() ) {
$query->the_post();
echo '<div class="onAir"><h3>Currently <span>On Air</span> : ';
the_title();
echo '</h3></div>';
}
wp_reset_postdata();
}
?>
I have Projects inserted as posts in my WordPress database. currently on my home, the last 3 published project is displayed. now my purpose is that I want first display the project which is expiring today than the last published project.
for example, there are 2 projects are expiring today than on the home page it will display 2 projects which are expiring today and 1 project which published last. it means a total of 3 projects will display.
please check below WP_query which returns last published project only
$args = array('post_type' => 'ignition_product', 'posts_per_page' => $project_count, 'paged' => $paged);
$newargs = apply_filters('project_query', $args);
$wp_query = new WP_Query($newargs);
the below query I try using meta key & value but no luck. "ign_fund_end" is stored a date as a string so I think that's why not comparing date.
my final goal is I described as above total 3 projects should display. first should be today expiring then after last published.
$args = array(
'post_type' => 'ignition_product',
'posts_per_page' => $project_count,
'paged' => $paged,
'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
array(
'key' => 'ign_fund_end', // Check the start date field
'value' => date('m/d/Y'), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'DATE' // Let WordPress know we're working with date
)
));
please check the below image for reference.
any solution appreciated.
Since your custom field ign_fund_end is not in MySQL date compatible format so that is the main reason for your WP_Query to not work in the expected way. My recommendation is to save a timestamp of end date in a custom field using save_post and then change the $args for WP_Query to work on that field.
Here is complete solution for your issue:
1: Save Timestampe in a Custom field
add_action( 'save_post', function( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
if( isset( $_POST['ign_fund_end'] ) && !empty($_POST['ign_fund_end']) ) {
$end_date = $_POST['ign_fund_end'];
$end_date = strtotime($end_date);
update_post_meta( $post_id, '__end_date', $end_date );
}
} );
2: Modify the $args
$args = array(
'post_type' => 'ignition_product',
'posts_per_page' => $project_count,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '__end_date',
'paged' => $paged,
'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
array(
'key' => '__end_date', // Check the start date field
'value' => strtotime('today'), // Set today's timestamp
'compare' => '>=', // Return the ones greater than today's date
'type' => 'NUMERIC'
)
));
You just need to remove type from the array parameters.
$args = array(
'post_type' => 'ignition_product',
'posts_per_page' => $project_count,
'paged' => $paged,
'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
array(
'key' => 'ign_fund_end', // Check the start date field
'value' => date('m/d/Y'), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'DATE' // Let WordPress know we're working with date
)
));
To:
$args = array(
'post_type' => 'ignition_product',
'posts_per_page' => $project_count,
'paged' => $paged,
'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
array(
'key' => 'ign_fund_end', // Check the start date field
'value' => date('m/d/Y'), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
//'type' => 'DATE' // Let WordPress know we're working with date
)
));
Note: The reason is that in table meta_value is not DATE type.
In the PHPMyAdmin, the default date type is:
2019-04-16
I am currently working on a Wordpress website with a Bbpress forum installed. I am using a shortcode to add all the popular topics on a (non-forum)page:
[bbp-single-view id="popular"]
I want to add a extra filter to this function and I can't figure out how to do this. I would like to filter on the meta value '_bbp_last_active_time', to check if there was any activity on a topic in the last month.
I found this script in the bbpress core, but I don't know how I can add another filter:
// bbpress.php line 672
bbp_register_view(
'popular',
__( 'Most popular topics', 'bbpress' ),
apply_filters( 'bbp_register_view_popular', array(
'meta_key' => '_bbp_reply_count',
'max_num_pages' => 1,
'orderby' => 'meta_value_num',
'show_stickies' => false
)
) );
I did try something like this with SQL and a custom shortcode, but I don't know how to convert this to a list of topics with the correct layout functions:
SELECT
wp_posts.post_parent, COUNT(*) as count, wp_postmeta.*
FROM
wp_posts, wp_postmeta
WHERE
wp_posts.post_type = 'reply'
AND
wp_postmeta.post_id = wp_posts.post_parent
AND
wp_postmeta.meta_key = '_bbp_last_active_time'
AND
wp_postmeta.meta_value > (NOW() - INTERVAL 1 MONTH)
GROUP BY
wp_posts.post_parent
ORDER BY
count DESC
LIMIT 5;
Thanks in advance! Help is much appreciated!
I already found a solution:
$date_now = date( 'Y-m-d H:i:s' );
$date=date_create($date_now);
date_sub($date,date_interval_create_from_date_string("30 days"));
$new_date = date_format($date,"Y-m-d H:i:s");
bbp_register_view(
'jaappopular',
__( 'Most popular topics', 'bbpress' ),
apply_filters( 'bbp_register_view_popular', array(
'meta_key' => '_bbp_reply_count',
'max_num_pages' => 1,
'orderby' => 'meta_value_num',
'show_stickies' => false,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_bbp_last_active_time' ,
'value' => $new_date,
'compare' => '>=',
)
),
)
) );
);
YEAH!
I have a wp website with lunch and dinner offers for the next day. The offers are entered as posts, are in there own category (id=4) and have two custom field value:
- menu_date - the date when the post should be visible
- menu_number - if it's lunch it's 1, if it's dinner is 2
I use this query to get the posts i need:
$args = array(
'post_type' => 'post',
'cat' => 4,
'meta_key'=>'menu_number', 'orderby' => 'meta_value_num', 'order' => ASC );
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while($query->have_posts()) {
$query->the_post();
$menu_date = get_post_custom_values( 'menu_date' );
$menu_number = get_post_custom_values( 'menu_number' );
$tommorow = date("dmY", time()+86400);
if ( $tommorow == $menu_date[0] && has_post_thumbnail() ) {
the_post_thumbnail('full', array( 'class' => 'img-responsive' ) );
if ($menu_number[0] == 1) {
Get Lunch!
}
else{
Get Dinner!
}
the_title();
}
}
} wp_reset_query();
Every day both lunch and dinner offers should be visible, but sometimes only lunch is shown, but after a few refreshes both are visible again.
Any way to improve the code so that this doesn't happen anymore? Thanks
Every day both lunch and dinner offers should be visible, but sometimes only lunch is shown, but after a few refreshes both are visible again.
It may be related to how you compute for tomorrow's date:
$tommorow = date("dmY", time()+86400);
Try changing it to as follows and see if that would work:
$tomorrow = date("dmY", strtotime("tomorrow"));
Try to check both at once in a loop. Like this :
`if ($menu_number[0] == 1) {
Get Lunch!
}
if ($menu_number[0] == 2){
Get Dinner!
}`
If this doesn't solve the problem, try debugging the values of $menu_number and $menu_date. See what is coming.
EDIT:
The meta query args should be like this: $tommorrow was incorrect in your args
'meta_query' => array(
array(
'key' => 'menu_date',
'value' => $tommorow
)
)
For Date Query:
$tomorrow = date("dmY", time()+86400);
$args = array(
'date_query' => array(
array(
'year' => $tomorrow["year"],
'month' => $tomorrow["mon"],
'day' => $tomorrow["mday"],
),
),
);
This will give you tomorrow's posts.
I figured it out:
I changed the $args like this. I added the menu_date here and I'm only retrieving the posts that have correct value.
$args = array(
'post_type' => 'post',
'cat' => 4,
'meta_query' => array(
array(
'key' => 'menu_date',
'value' => "$tommorow"
)),
'meta_key'=>'ordine_meniu', 'orderby' => 'meta_value_num', 'order' => ASC );
$query = new WP_Query($args);
I would create a custom loop in the home page that will display only the posts that contains a specific word in its custom field.I'l try to explain it with an example.
EX. I got a website of a Basketball team and 2 custom post types called "COACh" and "TRAINING".
In "COACH" i add the coach and its skills.
In "TRAINING" i add the exrcise's name,the description and in the custom meta box i add the time,the coach,the duration and the day of the execution.
Now,suppose that it's Monday i would display all the TRAININGS that contains the day Monday in its custom field.
Is it possible without a plugin like event calendar????
IS IT RIGHT?
$today = date("l");
$args = array( 'post_type' => 'palinsesto', 'posts_per_page' => -1);
$palinsesto = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'giorno ' ); // here day is key
if($day==$today)
while ($post->have_posts()) : $post->the_post(); ?>
Yes it is possible, I coded below roughly:
$args = array( 'post_type' => 'TRAINING', 'posts_per_page' => -1);
$training = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'day ' ); // here day is key
if($day=='Monday')
echo $post->post_title.'<br />';
echo $post->post_content.'<br />';
}
this one is the solution for my question...it works great
$today = strftime('%A');
$day = get_post_meta( $post->ID, 'giorno ' );
$my_query = new WP_Query(array(
'post_type' => 'palinsesto',
'meta_query' => array(
array(
'key' => 'giorno',
'meta-value' => $value,
'value' => $today,
'compare' => '=',
))));
if (have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>