I am having issues with the functionality of my code. The custom post should expire after a certain number of days. At the moment it’s set to 3 days just so I can test it.
But the posts are expiring even though the expiry date is before today's date, and I can’t figure out why?!
// Setup Cron Job Function to expire posts and send out emails.
function listing_expiry_date() {
// Get the current date
date_default_timezone_set('Europe/London');
$today = date('m/d/Y');
// Custom Post Type for listings, grab published posts
$args = array(
'post_type' => array( 'post_type_listings' ),
'post_status' => array( 'Publish' ),
'posts_per_page' => -1,
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
// Get the listing ID
$post_id = get_the_ID();
// Get listing post published date (not pending date)
$published_date = get_the_date( 'm/d/Y', get_the_ID() );
$expiry_date = date( 'm/d/Y', strtotime( '+3 days', strtotime($published_date) ) );
// If todays date is equal to or greater than the expiry date...
if ( ($today == $expiry_date) || ($today > $expiry_date) ) :
// change post status to 'expired'
$postdata = array(
'ID' => $post_id,
'post_status' => 'expired',
);
// Update post data
wp_update_post($postdata);
endif;
endwhile; //endwhile The Loop
endif; //endif The Loop
}
IT seems your issue would be how you're evaluating the dates. In this case, it would be best to use a Unix timestamp to evaluate which is greater.
function listing_expiry_date() {
// Get the current date.
date_default_timezone_set( 'Europe/London' );
$today = date( 'U' );
// Custom Post Type for listings, grab published posts.
$args = array(
'post_type' => array( 'post_type_listings' ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
);
// The Query.
$query = new WP_Query( $args );
// The Loop.
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
// Get the listing ID.
$post_id = get_the_ID();
$expiry_date = date( 'U', strtotime( '+3 days', get_the_date( 'U', $post_id ) ) );
// If Expiration date is < or = today.
if ( $expiry_date <= $today ) :
// change post status to 'expired'
$postdata = array(
'ID' => $post_id,
'post_status' => 'expired',
);
// Update post data.
wp_update_post($postdata);
endif;
endwhile; // endwhile The Loop.
endif; // endif The Loop.
}
Related
I need to change the last modified date and time of all of my blog's posts in WordPress. So far i have....
add_action( 'wp', 'asd' );
function asd()
{
$post_list = get_posts( array(
'post_per_page' => '-1'
) );
foreach ( $post_list as $post ) {
// $posts[] += $post->ID;
$postID = $post->ID;
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
echo $postID . ' ||| ' . $datetime . '<br>';
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts` SET `post_modified` = '" . $datetime . "' WHERE ID = " . $postID);
}
}
I get no errors or whatsoever. I am using "echo" for debugging purposes. I have two problems.
I have 6 posts and i get only 5
It seems that no update is happening in the database, for the last modified field
Any help would be appreciated.
you use this code.( paste it in your theme function.php)
add_action('init','change_mypost_date');
function change_mypost_date(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
$current_post = array(
'ID' => get_the_ID(),
'post_modified' => $datetime
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}
You are looking to update all of your posts' date to current datetime. So, the solution is without requiring any loops:
add_action( 'init', function () {
$hook = 'run_snippet_daily';
$args = array();
if ( ! wp_next_scheduled( $hook, $args ) ) {
wp_schedule_event( time(), 'daily', $hook, $args );
}
} );
add_action( 'run_snippet_daily', function () {
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts`
SET `post_date`='{$datetime}',
`post_modified` ='{$datetime}'
WHERE `post_type`= 'post' ");
} );
I am in search of the best way to display pages active posts categories and expired after a certain period. I want to display active posts or have no expiration date in the main loop and those that expire in a loop below.
For this I used the plugin advanced custom field ( expiration ) which define the expiration date. With loops below, I get the result I want, however, would help to improve. For example, in the loop of expired posts, the H2 tag appears on the page even if there are no expired posts, and if I move it under the condition "if ( $ exhale < $ blogtime )", it appears in all expired posts, what do not want, I want to appear only once above all expired posts.
Another question I have is whether these loops will somehow require more work from the server, since it will have to make new appointments.
<?php if ( have_posts() ) : ?>
<?php if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
$blogtime = date( 'm/d/Y H:i:s', current_time( 'timestamp', 0 ) );
$blogt = date( '0' );
$expire = get_field('expiration');
if( $expire > $blogtime ) {
get_template_part( 'template-parts/content', get_post_format() ); }
elseif( $expire < $blogt ) {
get_template_part( 'template-parts/content', get_post_format() ); }
// End the loop.
endwhile;
endif;
?>
<?php
$args = array(
'post_type' => array('post','news'),
'posts_per_page' => 15,
'cat' => $cat,
'meta_key' => 'expiration',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$eventloop = new WP_Query($args);
if ( $eventloop->have_posts() ) :
echo '<h2>Expired</h2>';
while ( $eventloop->have_posts() ) : $eventloop->the_post();
$blogtime = date( 'm/d/Y H:i:s', current_time( 'timestamp', 0 ) );
$blogt = date( '0' );
$expire = get_field('expiration');
if( $expire < $blogtime ) {
if ( $expire > $blogt ) {
get_template_part( 'template-parts/content', get_post_format() );
} }
endwhile;
endif;
?>
I would like to filter the WP_Query to retrive all woocommerce order (post_type = shop_order) with particular status that are older than 15 minutes.
I.e. All posts that have been last modified on or before (now - 15 minutes ) // Hope I'm clear.
Below What I've tried
function filter_where($where = '') {
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('completed')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
print_r("<pre>");
print_r($order);
print_r("</pre>");
endwhile;
However that returns all record, Seems like have to modify the $where query, how can i achieve that ?
Do you need to filter the posts_where? Can't you just use date query parameters? In your case, specifically before.
$args = array(
'date_query' => array(
array(
'before' => '15 minutes ago'
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
I can't test this right now, so can't verify if it would work. If you are modifying an archive then you should definitely adjust the query via pre_get_posts in lieu of creating new query.
try this change date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) to date('Y-m-d H:i:s', strtotime('-15 minutes'))
function filter_where($where = ''){
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d H:i:s', strtotime('-15 minutes')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
I have a list of the authors (Wordpress) on the site that appears on every page so the list exists outside of the loop.
I managed to show every authors' image with their names but I would like to get their latest post title that links to the post. The post title should only show when the post is not older than a month.
Any help would be appreciated.
Thanks
<?php
global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
$author_ids = $wpdb->get_results($query);
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
$post_link = get_permalink($curauth->ID);
// Set default avatar (values = default, wavatar, identicon, monsterid)
$main_profile = get_the_author_meta('mainProfile', $curauth->ID);
$hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
$award_profile = get_the_author_meta('awardProfile', $curauth->ID);
?>
You can use WP_Query to create a new loop for you instead. It accepts a cool date_query argument since version 3.7. Untested, but should work.
EDITED:
$args = array(
'showposts' => 1,
'orderby' => 'date',
'date_query' => array(
array(
'after' => array(
'year' => date( "Y" ),
'month' => date( "m", strtotime( "-1 Months" ) ),
'day' => date( "t", strtotime( "-1 Months" ) ),
),
'inclusive' => true,
)
) );
$query = new WP_Query( $args );
Then you can just run a regular loop
// run the loop with $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo 'Latest post: ' . get_the_title();
}
} else {
// no posts
}
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(); ?>