Wednesday, February 15, 2023
HomeWordPress Developmentplugins - Set the title of a customized put up robotically by...

plugins – Set the title of a customized put up robotically by utilizing information from customized fields?


The difficulty together with your code is that the get_metadata() operate returns an array of values, not a single worth. So if you concatenate $first and $final variables on this line $title = $first . ‘ ‘ . $final;, you’re really concatenating two arrays, which leads to the “Array Array” title.

To repair this, you should use the get_post_meta() operate as a substitute of get_metadata() to retrieve the primary and final identify values as follows:

$first = get_post_meta( $post_id, 'first_name', true ); //meta for first identify
$final = get_post_meta( $post_id, 'last_name', true );   //meta for final identify

The third parameter true within the get_post_meta() operate specifies that you just need to retrieve a single worth as a substitute of an array of values.

Here is the up to date code:

add_filter( 'save_post_practitioners', 'hexagon_practitioner_set_title', 
10, 3 );
operate hexagon_practitioner_set_title ( $post_id, $put up, $replace ){
//This quickly removes filter to stop infinite loops
remove_filter( 'save_post_practitioners', __FUNCTION__ );

//get first and final identify meta
$first = get_post_meta( $post_id, 'first_name', true ); //meta for first 
identify
$final = get_post_meta( $post_id, 'last_name', true );   //meta for final 
identify

$title = $first . ' ' . $final;

//replace title
wp_update_post( array( 'ID'=>$post_id, 'post_title'=>$title ) );

//redo filter
add_filter( 'save_post_practitioners', __FUNCTION__, 10, 3 );
}

This could set the put up title to “First Identify Final Identify” as you supposed.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments