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);
Related
Introduction
I have a blog where I am showing latest post within the last 365 days and only show 100 post. I also have the option for the user to select year (this is what im currently working on).
Question
How can I have it so when A user chooses a different year (from the selection) the query is set to show post from that year and remove the 100 post limit and show this is my blog page.
My Query
<?php
// gets info
$current_category = get_queried_object('post');
//$current_user =get_the_author_posts();
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => $current_category->term_id,
// 'author' => $current_user,
// 'tag' => '',
'posts_per_page' => 100,
'date_query' => array(
array(
// 'year' => '2019',
'after' => '-365 days',
'column' => 'post_date',
)
)
));
My Selection (year)
<select style="top: -4px;"
class=" btn-link col-12"
name="archive-dropdown"
onChange='document.location.href=this.options[this.selectedIndex].value;'>
<?php wp_get_archives('type=yearly&format=option'); ?>
</select>
My Blog Page
<?php
require "settings.php";
if ($wpb_all_query->have_posts())
:while ($wpb_all_query->have_posts())
:$wpb_all_query->the_post();
?>
content
<?php endwhile; ?>
<?php endif; ?>
I was trying a statement in the query but could not see how to get year selected , but then if i set the year to a var depending on the option maybe that's better>?
This will give you all posts (no limit to 100), for the year 1999. Change $yearToLookFor to the year you need.
<?php
//here is from a $_GET parameter
$yearToLookFor = $_GET['year'];
// gets info
$current_category = get_queried_object('post');
//$current_user =get_the_author_posts();
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => $current_category->term_id,
// 'author' => $current_user,
// 'tag' => '',
'date_query' => array(
array(
'year' => $yearToLookFor,
)
)
));
In this example, the year got taken from a url query, like this: https://yourwebiste.zyx/?year=1999
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
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(); ?>
I want to create a query where all posts should be displayed where the custom field "expiration_date' is larger than the date from today.
Short form: if the expiration date of the post is reached, it should no longer displayed in the query
I tried with this snippet:
<?php
$today = date("Y-m-d");
$args= array(
'tag' => 'Pinnwand',
'meta_query' => array(
'key' => 'expiration_date',
'type' => 'DATE',
'value' => $today,
'compare' => '>'
)
);
$my_query = new WP_Query($args); ?>
the expiration date is in the format (2014-10-04) for example.
But I tried also the format "Ymd" on both sides, change the compare type, or set the type as "NUMERIC" and nothing helps. The result is, that the post will always be displayed.
It would be great if somebody could help me!
Okay I found the mistake!
The correct query needs one more array(). I don't know exactly why, but in other cases the query couldn't work with it. So here is the code
$args= array(
'tag' => 'Pinnwand',
'meta_query' => array(
array(
'key' => 'expiration',
'type' => 'DATE',
'value' => $today,
'compare' => '>'
),
),
);
$my_query = new WP_Query($args); ?>
You could do a two-query exclusion. First query finds all the posts you want to exclude, then loop through those and store the post IDs in an array, then call second query excluding them using 'post__not_in' with the ID array as the argument.
I would use the $wpdb object for this because it's very efficient.
For reference material:
http://codex.wordpress.org/Class_Reference/WP_Query and
http://codex.wordpress.org/Class_Reference/wpdb
This chunk of code SHOULD do what you're trying to do or get you close to it, and I kept your tag part of the query in there too.
// first query to find exclusions
$today = date("Y-m-d");
$exclusions = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE expiration_date < ".$today);
if ( $exclusions ) {
foreach ( $exclusions as $exclusion ) {
$excludeIDs[] = $exclusion->ID;
}
}
// second query using exclusion array
$args= array(
'tag' => 'Pinnwand',
'post__not_in' => $excludeIDs,
);
$my_query = new WP_Query($args);
You can do it simpler, as stated on http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters:
$args= array(
'tag' => 'Pinnwand',
'meta_key' => 'expiration',
'meta_type' => 'DATE',
'meta_value' => $today,
'meta_compare' => '>'
);
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(); ?>