Saltar navegación

[Resuelto] Problem in view post author

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I've a custom post type "accommodation".
I've a field repetable group "room" taht is displayed in the accommodation page.

Each accommodation has a different author (accommodation manager role).

In mt WP backend I've add as administrator some rooms fro some accommodation.

The problem is that now I'm the author of this room and not the accommodation manager.

How can I set it?

Solution:

It is expected result, Toolset repeatable group is actually a hidden custom post type, and you are using administrator to setup the "room" item in admin side, so the "room" item author will be setup as current user: administrator.

In your case, I suggest you try these:
When you edit a single "accommodation", add/edit a room item(post), save the "accommodation" post, use WordPress action hook "save_post" to trigger a custom function, in this function update the room post's author as "accommodation" post author. For example:

https://toolset.com/forums/topic/problem-in-view-post-author/#post-1146368

Relevant Documentation:

https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created hace 6 años, 3 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por Luo Yang hace 6 años, 3 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1146342

Hi,

I've a custom post type "accommodation".
I've a field repetable group "room" taht is displayed in the accommodation page.

Each accommodation has a different author (accommodation manager role).

In mt WP backend I've add as administrator some rooms fro some accommodation.

The problem is that now I'm the author of this room and not the accommodation manager.

How can I set it?

Please conider that rooms are not custom post but only fields and I've set it via backend

#1146368

Hi,

It is expected result, Toolset repeatable group is actually a hidden custom post type, and you are using administrator to setup the "room" item in admin side, so the "room" item author will be setup as current user: administrator.

In your case, I suggest you try these:
When you edit a single "accommodation", add/edit a room item(post), save the "accommodation" post, use WordPress action hook "save_post" to trigger a custom function, in this function update the room post's author as "accommodation" post author. For example:

function update_room_author( $room_id ) {
	$room_cpt_slug = 'room-group';
    if ( get_post_type( $room_id ) == $room_cpt_slug ) {
        // Get accommodation from RelatedItem
		$accommodation_id = toolset_get_related_post( $room_id, $room_cpt_slug, 'parent' );
        $accommodation_post = get_post($accommodation_id);
      	$accommodation_author_id = $accommodation_post->post_author;
        //  update room post author
      	$args = array(
          'ID'=> $room_id, 
          'post_author' => $accommodation_author_id,
        );
		remove_action( 'save_post', 'update_room_author', 10 );
        wp_update_post( $args );
		add_action( 'save_post', 'update_room_author', 10, 2 );
     }   
}
add_action( 'save_post', 'update_room_author', 10, 2 );

Please replace "room-group" with your repeatable group room slug, you can get it by editing repeatable group "room", option value "Group slug".

#1146589

Perfect! Thank you for your help Luo

#1146777

You are welcome