Support

Account

Forum Replies Created

  • I solved it by adapting John’s code. Thank you.

    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php 
                	if (is_user_logged_in() && current_user_can('cliente')) {
                	// Get the current user
                		$current_user_id = get_current_user_id();
                		$args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		);
                		$posts = get_posts($args);
                		if ($posts) {
                		  $post_id = $posts[0]->ID;
                		}
    
         	       acf_form(array(
    	               	'post_id'  =>  $post_id,
    	                'submit_value' => __('Actualizar Mi perfil'),
    	           ));
                	}
                	?>
    			<?php endwhile; ?>
    
  • 
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php 
                	if (is_user_logged_in() && current_user_can('cliente')) {
                	// Get the current user
                		$current_user_id = get_current_user_id();
                		$args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		);
                		$posts = get_posts($args);
                		if ($posts) {
                		  $post_id = $posts[0]->ID;
                	       acf_form(array(
    		                	'post_id'  =>  $post_id,
    			                'submit_value' => __('Actualizar Mi perfil'),
    		                ));
                		}
                	}
                	?>
    			<?php endwhile; ?>
    
  • Thank you very much for your answer.

    I had the problem that you were marking me with doing it through a short code, so I am doing it as a template.

    I never did it this way, it marks me an error and I don’t know what it is.

    In the loop I have the following:

    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php if (is_user_logged_in() && current_user_can('cliente')) : ?>
                		<?php $current_user_id = get_current_user_id(); ?>
                		<?php $args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		); ?>
                		<?php $posts = get_posts($args); ?>
                		<?php if ($posts) 
                		  $post_id = $posts[0]->ID;
                	     ?>
                    : ?>
    			<?php endwhile; ?>
    
  • Is the the only post that is created with with the post_author set to the user ID?

    In the “customers” custom post type , yes.

  • How is the post created?

    
    /*
     * Agregar entrada personalizada "clientes" cuando se registra un nuevo usuario
     */
    
    add_action( 'user_register', 'agregar_cp_clientes_usuario', 10, 1 );
    
    function agregar_cp_clientes_usuario( $user_id ) {
       // Get user info
        $user_info = get_userdata( $user_id );
        $user_roles = $user_info->roles;
    
        // New code added 
        $this_user_role = implode(', ', $user_roles );
    
        if ($this_user_role == 'cliente') {
    
            // Create a new post
            $user_post = array(
                'post_title'   => $user_info->user_nicename,
                'post_author'   => $user_info->ID,
                'post_status'  => 'publish', // <- here is to publish
                'post_type'    => 'clientes', // <- change to your cpt
            );
            // Insert the post into the database
            $post_id = wp_insert_post( $user_post );
        }
    }
    

    What is it that causes the user and the post that is automatically created related to each other?

    They are related by the author of the post.
    That is, the post is added automatically, when a new user registers.

  • How can I do that?

    You also need to use the post id of the post that the user can edit, so you’ll need to get the post related to the user and set “post_id” in acf_form or ACF will save the values to whatever page is currently being displayed.

  • Thanks for your answer.

    I was thinking of doing it with a short code on a page in the frontend.

    So far I have something like this:

    
    function profile_user_frontend($atts = array() ) {
    	if (is_user_logged_in() && current_user_can('cliente')) {
    	// Get the current user
    		$current_user_id = get_current_user_id();
    		acf_form([
    			'field_groups' => [group_60c25840f1e02],
    			'post_title'    => false,
            		'post_content'  => false,
            		'submit_value'  => __('Update meta')
    		]);
             } else {
                    return site_url('/ingreso/');
    }
    add_shortcode('profile_user_frontend','profile_user_frontend_shortcode');
    
Viewing 7 posts - 1 through 7 (of 7 total)