NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Easy
Last modified : Mar 05 2023
Estimated reading time : 3 minutes, 54 seconds - 118 words
Share the post "Create a Shortcode for Modified Posts"
With this tutorial, we will create a shortcode that will fetch the most recent posts or post types.
In our example, we will fetch the posts edited in the last 15 days.
However, we will display all published posts in the last 15 days. Afterward, we will tweak our query to adapt it to edited/modified posts.
Displaying posts published in the last 15 days
/**************************** Last Posts 15 days ********************************/ add_shortcode( 'lastaddoposts', 'addo_list_posts_publied' ); function addo_list_posts_publied(){ $date_range = strtotime ( '-15 day' ); // Custom WP query query $args = array( 'post_type' => array('post'), 'post_status' => 'publish', 'order' => 'DESC', 'date_query' => array( array( 'after' => array( 'year' => date('Y', $date_range ), 'month' => date('m', $date_range ), 'day' => date('d', $date_range ), ), ) ) ); $query = new WP_Query( $args ); echo '<ul style="padding:0">'; if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); echo '<div class="col-md-3" style="display:inline-block;">'; echo '<li style="position:relative">'; echo '<a style="display:block;background-color: #490b3d;padding: 10px 25px;color: white;" href="' . get_the_permalink() . '">'; echo the_title(); echo'</a>'; echo '<span style="padding:0px 5px;background:#f1b814;color:white;position:absolute;top:-10px;right:0;font-size:11px">' . get_the_time('d-m-Y') . '</span>'; echo '</li>'; echo '</div>'; } } else { echo 'Il n'y a eu aucune publication ces dernières semaines'; } wp_reset_postdata(); echo '</ul>'; }
Shortcode within pages
[lastaddoposts]
Shortcode within templates
<?php echo do_shortcode( '[lastaddoposts]' ); ?>
We will now adapt our query and display the last modified posts within the last 15 days.
Displaying posts modified in the last 15 days
/**************************** Last Posts modified 15 days ********************************/ add_shortcode( 'postsaddomodified', 'addo_list_posts_modified' ); function addo_list_posts_modified() { // query args $args = array( 'posts_per_page' => '10', 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'modified', 'order' => 'DESC', 'ignore_sticky_posts' => '1', 'caller_get_posts' => 1, 'date_query' => array( 'relation' => 'OR', array( 'column' => 'post_date', 'after' => '-1 days' ), array( 'column' => 'post_modified', 'after' => '-15 days' ) ) ); // query $updated = new WP_Query($args); ?> <ul style="padding:0"> <?php if ( $updated->have_posts() ) : ?> <?php while ( $updated->have_posts() ) : $updated->the_post(); ?> <div class="col-md-3" style="display:inline-block;padding:0px"> <li style="position:relative"> <a style="display:block;background-color: #490b3d;padding: 10px 25px;color: white;margin:0 0 10px 0" href="https://famillebreboin.ca/photos/"> <?php the_title(); ?> </a> <span style="padding:0px 5px;background:#f1b814;color:white;position:absolute;top:-10px;right:0;font-size:11px"> <?php get_the_time('d-m-Y'); ?> </span> </li> </div> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <?php echo 'Il n'y a eu aucun album mis à jour'; ?> <?php endif; ?> </ul> <?php }
Shortcode within pages
[postsaddomodified]
Shortcode within templates
<?php echo do_shortcode( '[postsaddomodified]' ); ?>