Support

Account

Home Forums General Issues how to display the author's name

Solving

how to display the author's name

  • Hello everyone, I created a form with ACF custom fields to enter cooking recipes, everything goes well in the database, but now I would like to display the title of the recipes and the author of the recipe
    But when I query the database with this query.
    Are all recipes in the name of the administrator?

    <?php
    		// 1. On définit les arguments pour définir ce que l'on souhaite récupérer	
    	$args = array(
        'post_type' => 'recette',
        
      
    );
     // 2. On exécute la WP Query
    // The Query
    $the_query = new WP_Query( $args );
     // 3. On lance la boucle !
    // The Loop
    if ( $the_query->have_posts() ) {
        
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
            echo '<li>' . get_the_author() . '</li>';
            
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();		
    			
    		?>	
  • Hi @flexi2202,

    Is your form only visible to logged in users? I assume you’re logged in as non-admin?

    I think you need to use the acf_form() parameters

    On of the parameters is: html_after_fields.

    As you’re logged in, you can access the user ID:
    $user_id = get_current_user_id();

    So you could then use a hidden value:
    'html_after_fields' => '<input type="hidden" name="acf[author_id]" value="'.$user_id.'"/>',

    Using acf/save_post

    You can then grab the hidden value and updated the post author:

    add_action('acf/save_post', 'my_acf_save_post');
    function my_acf_save_post( $post_id ) {
    	
    	$arg = array(
    		'ID' => $post_id,
    		'post_author' => $_POST['acf']['author_id']
    	);
    	wp_update_post( $arg );	
    	
    }

    Code is untested but should point you in the right direction

  • Hello
    a big thank you for your
    but I just realized that I forgot to check author in the parameters of CPT, so everything works, thank you again

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.