Add tag to post type if it is within 3 months of current date
Add tag to post type if it is within 3 months of current date
I have post types called articles and I want to add a tag to the post, if it was published -100 days of the current date.
This is so I can filter these and showcase the most recent posts, and the user can filter the tag on the posts to see recently added posts (this is posts -100 days of the current date).
if ( $query->have_posts() ) {
?>
Found <?php echo $query->found_posts; ?> Results<br />
<div class='search-filter-results-list'>
<?php
while ( $query->have_posts() ) {
$query->the_post();
// Get post published date
$post_date = get_the_date('Ymd');
$current_date = current_time('Ymd');
$future_date = date('Ymd', strtotime('-100 days'));
// Check if post published date is -100 days before the current date
$class_to_add = ($post_date <= $current_date && $post_date >= $future_date) ? ' your-custom-class' : '';
?>
<div class='search-filter-result-item<?php echo esc_attr($class_to_add); ?>'>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><br /><?php the_excerpt(); ?></p>
<?php if ( has_post_thumbnail() ) : ?>
<p><?php the_post_thumbnail( 'small' ); ?></p>
<?php endif; ?>
<p><?php the_category(); ?></p>
<p><?php the_tags(); ?></p>
<p><small><?php the_date(); ?></small></p>
<hr />
</div>
<?php
}
?>
</div>
<?php
}
Does anyone have any suggestions?
Thank you!
This currently checks the post date, and if the post date is -100 to the current date. It will add a class onto it.
if ( $query->have_posts() ) {
?>
Found <?php echo $query->found_posts; ?> Results<br />
<div class='search-filter-results-list'>
<?php
while ( $query->have_posts() ) {
$query->the_post();
// Get post published date
$post_date = get_the_date('Ymd');
$current_date = current_time('Ymd');
$future_date = date('Ymd', strtotime('-100 days'));
// post published date is -100 days before current date
$class_to_add = ($post_date <= $current_date && $post_date >= $future_date) ? ' your-custom-class' : '';
?>
<div class='search-filter-result-item<?php echo esc_attr($class_to_add); ?>'>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><br /><?php the_excerpt(); ?></p>
<?php if ( has_post_thumbnail() ) : ?>
<p><?php the_post_thumbnail( 'small' ); ?></p>
<?php endif; ?>
<p><?php the_category(); ?></p>
<p><?php the_tags(); ?></p>
<p><small><?php the_date(); ?></small></p>
<hr />
</div>
<?php
}
?>
</div>
<?php
}
February 4, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
February 4, 2024