Is there any function to get yesterday published all post ids?
I always need yesterday or specific days post ids.
Example: today is: 01/04/2017 , i need all published post ids which published on 31/03/2017
Try this code.
<?php
$today = getdate();
$y_posts_ids = array();
$agrs = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'] - 1,
),
),
);
$loop = new WP_Query( $agrs );
if( $loop->have_posts() ) :
while( $loop->have_posts() ) : $loop->the_post();
$y_posts_ids[] = get_the_ID();
endwhile;
endif;
wp_reset_postdata();
?>
You can use the date parameters of get_posts to find this:
<?php
// Get the date pieces using the timezone you set in WordPress, minus one day
$yesterday = getdate( current_time() - DAY_IN_SECONDS );
// Query for posts from yesterday
$yesterdays_posts = get_posts( array(
'date_query' => array(
array(
'year' => $yesterday[ 'year' ],
'month' => $yesterday[ 'mon' ],
'day' => $yesterday[ 'mday' ]
)
),
'nopaging' => true
) );
This will give you an array of post objects from yesterday. nopaging means "give me all of them". Now, you can iterate through the results. If you just need the IDs, you can add them to an array like this:
<?php
$yesterdays_posts_ids = array();
if( $yesterdays_posts ){
foreach( $yesterdays_posts as $yesterdays_post ){
$yesterdays_posts_ids[] = $yesterdays_post->ID;
}
}
Related
I am using Wordpress for a long time and now try to get something out of its API.
I looked at https://developer.wordpress.org/rest-api/reference/posts/ and find my desired request is not there:
I want to extract posts posted on the same date (as today) but years back. For example, on Jan 8th 2019, I want to get the posts with the same date (Jan 8th) in previous years.
Now I can do a SQL and select directly from the database but I want to see if an API can do it.
i believe what you might need is this query:
$date = getdate();
$args = array(
'post_type' => 'post',
'date_query' => array(
'before' => array(
'year' => $date['year'] - 1,
'month' => $date['mon'],
'day' => $date['mday']
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ):
while ( $query->have_posts() ): $query->the_post();
the_title();
endwhile;
endif;
This will return all post titles before 01/07/2018
Read more about date_query here:
https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
If you want to fetch the posts just in same day on year back following code will be right :
$year = date( 'Y' );
$today = getdate();
$args = array(
'date_query' => array(
array(
'year' => $year,
'compare' => '<',
),
array(
'day' => $today['mday'],
'compare' => '=',
),
array(
'month' => $today['mon'],
'compare' => '=',
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ):while ( $query->have_posts() ): $query->the_post();
the_title();
endwhile;endif;
I have edited my theme on archive.php for show only a specific post and exclude a post with specific meta_key:
$posts_to_exclude_args = array(
'meta_key' => 'vip_box',
);
$posts_to_exclude = new WP_Query( $posts_to_exclude_args );
$to_exclude = array();
while ( $posts_to_exclude->have_posts() ) : $posts_to_exclude->the_post();
$to_exclude[] = $post->ID;
endwhile;
wp_reset_postdata();
$lastupdated_args = array(
'post__not_in' => $to_exclude,
'author__in' => $author,
'category__in' => $terms,
'posts_per_page' => 12,
'has_archive' => true,
'paged' => $paged,
);
$lastupdated_loop = new WP_Query( $lastupdated_args );
query_posts( $lastupdated_args );
<?php if ( have_posts() ) : ?>
And its perfect but now if i open a date link/url mywebsite.com/2017/06 it show all post and not only post in this date, why?.
Please can you help me?
I have fixed with this code, but i think there is a best solution than this. =)
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
$day = get_query_var('day');
'date_query' => array(
array(
'year' => $year,
'monthnum' => $monthnum,
'day' => $day,
),
),
);
I am trying to create a custom loop from the metadata I have entered into the posts.
<?php $args = array (
'post_type' => array( 'movies' ),
'order' => 'DESC',
'order_by' => 'get_post_meta( get_the_ID(), "released", true )',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
bd_pagination();
while ( $query->have_posts() ) : $query->the_post(); ?>
I so so need help plz
You need to tweak your parameters a bit. I'm assuming the "released" is a date field? If not (eg, if it's a timestamp), use a numeric orderby instead. But this should set you on your way:
<?php
$args = array(
'post_type' => array( 'movies' ),
'orderby' => 'meta_value_date',
'meta_key' => 'released',
'meta_type' => 'DATE'
);
$query = new WP_Query( $args );
I'm trying to query posts based on a custom field, and then display them in a loop. I've checked and double checked my code against the codex and other sources, but the query still does not appear to be working. What am I doing wrong?
Stripped down to the essentials, my code looks like this:
<?php
$args = array(
'meta_key' => 'my_custom_field'
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ) { ?>
<p>Success, we have posts!!!</p>
<?php } else { ?>
<p>Uh Oh, No posts!!!</p>
<?php } ?>
The conditional statement is dropping through and returning "Uh Oh, no posts".
I've checked the postmeta table, and there are definitely posts that contain the meta_key _my_custom_field. I have tried the query both with and without leading underscore.
What am I doing wrong?
I use this for search a date between two custom dates field in my custom post type "porfolio", i think that you are in a similar situation:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => '10',
'meta_query' => array(
array('key' => 'portfolio_start_date', 'value' => data_to_db2($ricerca_data), 'compare' => '<=', 'type' => 'NUMERIC'),
array('key' => 'portfolio_end_date', 'value' => data_to_db2($ricerca_data), 'compare' => '>=', 'type' => 'NUMERIC')
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$post_count = wp_count_posts();
while ( $the_query->have_posts() ) {
// DO WHAT YOU WANT
}
}
My advice is to use meta_query in $args array
For those familiar with the ACF plugin...
I have some events posts that are currently displaying in post order (see code below). I would like them to display in the order specified by the date-picker.
Can anyone tell me what to amend in the following - I have tried the documentation on the site, but my PHP is basic.
It says I need to add
'orderby' => 'meta_value_num',
But no joy.
<?php function le_whatson_aside() {
//THis loop is for the CPT
$args = array(
'post_type' => 'events', // enter your custom post type
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page'=> '10', // overrides posts per page in theme settings
'tax_query' => array(
array(
'taxonomy' => 'audience', //name of custom taxonomy
'field' => 'slug',
'terms' => 'everyone' //name of category
)
)
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
?>
<div>
<h2>What's On</h2>
</div>
<div class="whatson entry-content">
<?php
while( $loop->have_posts() ): $loop->the_post(); global $post;
?>
<p class="whatson-date"><?php echo date("dS F Y",strtotime(get_field('date')));?></p>
<h4 class="whatson-title"><?php echo get_the_title(); ?></h4>
<?php
endwhile;
?>
</div>
<?php
endif; }
Thanks all.
Try
orderby=date or `post_date`
If not the easiest way is to save your custom field 'startdate' as a unix timestamp. To do this, add the following to your theme's functions.php
// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'startdate', true);
if($startdate) {
$dateparts = explode('/', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
The do:
$today = time();
$args = array(
'post_type' => 'events',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$events = $query->posts;
Got it from here
I spent hours looking for this and I can confirm it works. See my code below.
If you're trying for a loop on a page with other loops, with a bunch of template parts in there, and you would also like to sort by a category, it goes :
$today = time();
$the_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'start',
'value' => $today,
'compare' => '>=',
)
),
'tax_query' => array(
array (
'taxonomy' => 'the_taxonomy',
'field' => 'slug',
'terms' => 'the-name'
)
),
'meta_key' => 'start',
'orderby' => 'meta_value',
'order' => 'ASC',
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'content-events' );
endwhile;
wp_reset_postdata();
Of course, you have to include Unix the function beforehand.
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'start', true);
if($startdate) {
$dateparts = explode('_', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
I would approach it like this. Create a "named" meta query and then order by that query. The meta query uses "EXITS" to filter out posts that don't have a date set. This works with the ACF date picker when the dates are saved to the database using the default format d/m/Y. This approach also works with the datetime picker.
$query = new WP_Query([
"meta_query" => [
"custom_date" => [
"key" => "date",
"compare" => "EXISTS",
"type" => "DATETIME",
]
],
"orderby" => [
"custom_date" => "ASC",
],
]);
Be sure to update the value for key to whatever your ACF field name is.