• Hi there, I can’t find any topic discussing my needs, so I create this topic.
    My needs: automatically create a daily post with the summary of all the posts published in the last 24 hours.
    What I can do so far:

    • I have created a shortcode that returns an unordered list with all the linked titles of the posts published in the last 24 hours. If I echo that shortcode on php I can see the list.
    • I can create a new post with the title desired (including the day-month-yar of creation) using wp_insert_post().

    What I can not do (so in this I need help):

    1. To insert in the post_content attribute the shortcode (I include the code used for the wp_insert_post)
    2. That every day at 20:00 WP creates a new post using the wp_insert_post().

    This is the code I use to create the new post:

    <?php 
    $content = do_shortcode( '[summary]' );
    $date = date("d/m/Y");
    $title_post = "Resumen diario - " .$date;
    
    $id = wp_insert_post(array(
      'post_title'=>$title_post, 
      'post_type'=>'resumen', 
      'post_status'=>'publish',
      'post_content'=> $content,
    ));
    ?>
    

    Thanks a lot for your help.

    • This topic was modified 4 years, 6 months ago by alexgagi.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use a scheduled event to run your post insertion code on a daily basis.

    Include your shortcode as plain text as part of the desired post content. Something like:
    $content = 'My diary for ' . date("d/m/Y") . '<br>[my-shortcode date="' . time() . '"]';

    Thread Starter alexgagi

    (@alexgagi)

    Thanks a lot for your answer, @bcworkz.
    I have achieved to create a cron job that executes my function using the WP Crontrol plugin, so the part of creating new post every day is solved. This is the function triggered:

    add_action( 'esfera_custom_cron', 'esfera_daily_summary' );
    
    function esfera_daily_summary() {
    	$content = do_shortcode( '[summary]' );
    	$date = date("d/m/Y");
    	$title_post = "Resumen diario - " .$date;
    
    	$id = wp_insert_post(array(
    	  'post_title'=>$title_post, 
    	  'post_type'=>'resumen', 
    	  'post_status'=>'publish',
    	  'post_content'=> $content,
    	));
    }

    The post is published and the title and post_type is ok, but post_content shows nothing, and I don’t know how to transform my shortcode into plain text as you suggest. This is my shortcode code:

    function daily_summary() {
      	$args = array(
    	    'date_query' => array(
    	        array(
    	            'column' => 'post_date_gmt',
    	            'after' => '60 hours ago',
    	        ),
    	    ),
    	    'posts_per_page' => -1,
    	);
    	$the_query = new WP_Query( $args );?>
    	<ul>
    	<?php
    	// Start our WP Query
    	while ($the_query -> have_posts()) : $the_query -> the_post(); 
    	// Display the Post Title with Hyperlink
    	?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
    	<?php 
    	// Repeat the process and reset once it hits the limit
    	endwhile;
    	wp_reset_postdata();
    	?>
    	</ul>
    <?php }
    add_shortcode('summary', 'daily_summary');

    Thanks for your help.

    Moderator bcworkz

    (@bcworkz)

    Adding content with a shortcode is as I described, but even simpler. I assumed it would pass a timestamp, but using date_query within the shortcode is even better.
    $content = '[summary]';

    I advise you to not do_shortcode() to create each post’s content. While it will work, you end up duplicating content. It becomes difficult to keep data in sync, plus it’s inefficient. Shortcodes are intended to be “done” when the post is output, not as it is saved.

    The issue is your shortcode handler does not correctly manage output. It is directly generating output, which is incorrect. All output must be collected into a single variable which is returned from your handler function for WP to echo out. Because your handler has a lot of mixed HTML and PHP, it’s probably simplest to collect output by using the PHP output buffer. Start the buffer before generating output. At the end, get/flush the buffer and return the buffer content.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create a daily post programatically’ is closed to new replies.