Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp_insert_post( $args ) #17200

Open
DietteJanssen opened this issue Aug 26, 2019 · 10 comments
Open

wp_insert_post( $args ) #17200

DietteJanssen opened this issue Aug 26, 2019 · 10 comments
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.

Comments

@DietteJanssen
Copy link

DietteJanssen commented Aug 26, 2019

i have searched through the issues/related issues and i have been unable to find a solution...
i need to include the gutenberg registered template when creating a new post programmatically.

i have registered a custom post type "credits" via php which includes the following arguments:

'show_in_rest'          => true, 
'rest_base'         	=> 'credits', 
'rest_controller_class' => 'WP_REST_Posts_Controller', 
'template' => array(
    array( 'core/image', array(
        'align' => 'left',
    ) ),
    array( 'core/heading', array(
        'placeholder' => 'Add Author...',
    ) ),
    array( 'core/paragraph', array(
        'placeholder' => 'Add Description...',
    ) ),
),

i am unable to programmatically add a new post which includes the gutenberg "template" array.

i have been able to output the following:

$post_content = '<!-- wp:paragraph --><p>Completed: '.$data['course']->post_title.'</p><!-- /wp:paragraph -->';

$my_post = array(
  	'post_type'		=> 'credits',
  	'post_title'    	=> 'Course Completed',
  	'post_content'  	=> $post_content,
  	'post_status'   	=> 'publish',
  	'meta_input'   	=> array(
  		'credits_user_id' => $user_id,
  		'credits_number_of_credits' => $course_points,
  		'credits_approved' => true, 
    ),

wp_insert_post( $my_post );

... which pulls in the gutenberg paragraph block successfully (rather than pulling it into the "classic" editor). however, i need it to pull in the template of blocks the same way it does when adding a new post from the wordpress backend.

for context, using php i have hooked into learndash:

defined( 'ABSPATH' ) || exit;

if ( !class_exists( 'LearndashCourseCompletion' ) ) {

	class LearndashCourseCompletion {

		public function __construct() {

			/* functions
			-------------------------------------------------------------*/
			// register
			add_action( 'learndash_course_completed', array( $this, 'dj_learndash_course_completed' ));
		}

		public function dj_learndash_course_completed( $data ) {

			$user_id = $data['user']->data->ID;
			
			$course_id = $data['course']->ID; 
			$course_points = '';
			$args = array(
			    'post_type' => 'sfwd-courses',
			    'p'			=> $course_id,
			);
			$loop = new WP_Query($args);

			if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();

				$course_points = get_post_meta( get_the_ID(), 'course_points', true );
				
				// Check if the custom field has a value.
				if ( ! empty( $course_points ) ) {
				    
					$post_content = '<!-- wp:paragraph --><p>Completed: '.$data['course']->post_title.'</p><!-- /wp:paragraph -->';

				    // Create post object
					
					$my_post = array(
					  	'post_type'		=> 'credits',
					  	'post_title'    	=> 'Course Completed',
					  	'post_content'  	=> $post_content,
					  	'post_status'   	=> 'publish',
					  	'meta_input'   	=> array(
					  		'credits_user_id' => $user_id,
					  		'credits_number_of_credits' => $course_points,
					  		'credits_approved' => true, 
					    ),
					);
					
					// Insert the post into the database
					wp_insert_post( $my_post );
				}

			endwhile; else: 
  			endif; 
  			wp_reset_postdata(); 

		} // end function
		
	} // end class	
}

new LearndashCourseCompletion();   

... i need to find a solution to achieve this, even if it requires using a different method ( other than wp_insert_post ) to achieve the desired result. any help or direction would be appreciated.

@swissspidy swissspidy added the [Feature] Templates API Related to API powering block template functionality in the Site Editor label Aug 27, 2019
@linusx
Copy link

linusx commented Feb 11, 2020

+1

6 similar comments
@akaufman3
Copy link

+1

@mwilliamson-creedinteractive

+1

@bratvanov
Copy link

+1

@niklaspollonen
Copy link

+1

@chvillanuevap
Copy link

+1

@omarkasem
Copy link

+1

@omarkasem
Copy link

omarkasem commented Mar 25, 2021

Here's a dirty workaround that worked with my case.
You create a page template and put your default template blocks in it.
Then you do that

$query = new WP_Query(array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'template-defalut.php',
        'post_type' => 'page',
        'fields'=>'ids',
    ));
    $posts = $query->posts;
    if(empty($posts)){return;}
    $content = get_post_field('post_content', $posts[0]);
    wp_update_post(array(
      'ID'=>478,
      'post_content'=>$content
    ));

For me it worked as I wanted to publish many posts at once with a certain template blocks, I hope that would help until this feature is applied.

@lunule
Copy link

lunule commented Feb 27, 2023

I didn't test it with core blocks, but the below method works fine with ACF blocks (so I would think it does with core stuff as well).

1. Post type registration

function cpt_mac_submenus() {

	$labels = array( //... );
	$args 	= array(
		// ...
		'template' => array(
			array( 'acf/submenus--title-and-description', array(
				'lock' => array(
					'move'   => true,
					'remove' => true,
				),
			) ),
			array( 'acf/submenus--output-navigation', array(
				'lock' => array(
					'move'   => true,
					'remove' => true,
				),
			) ),
			array( 'acf/submenus--single-image', array(
				'lock' => array(
					'move'   => true,
					'remove' => true,
				),
			) ),
		),
		'template_lock' => 'all',
	);

	register_post_type( 'mac-submenus', $args );

}
add_action( 'init', 'cpt_mac_submenus' );

2. Insert (custom post type) post with default content

$content = '<!-- wp:acf/submenus--title-and-description /--><!-- wp:acf/submenus--output-navigation /--><!-- wp:acf/submenus--single-image /-->';

$new_submenu_post_args_Arr = array(
	'post_title'    => $title,
	'post_type' 	=> 'mac-submenus',
	'post_status'   => 'draft',
	'post_author'   => 1,
	'post_content' 	=> $content,
);

wp_insert_post( $new_submenu_post_args_Arr );	

Basically, the $content variable should be set up with "blank" blocks, no content, no configuration, just the skeleton - as a result, the output is 100% the same thing configured in the register_post_type() arguments array.

@chrillep
Copy link

chrillep commented Jun 30, 2023

i believe wp_insert_post needs to be extended to have knowledge of the template.
This is something the wp core team should do.

https://developer.wordpress.org/reference/functions/wp_insert_post/
the wp_insert_post has arg for page_template but this is not the same as a gutenberg template.

so either use the registered gutenberg template, if no content is inserted or add means to merge data with the blocks inside the gutenberg template :)

ref:

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests