Wordpress API to get posts on a specific date - wordpress

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;

Related

WP_Query for Archive Page Date not work

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,
),
),
);

wordpress how to get yesterday published post ids

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;
}
}

custom post type loop not working

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 );

wordpress advanced custom fields order posts by date-picker

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.

Wordpress Custom Post Meta Query

I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one

Resources