Support

Account

Home Forums Front-end Issues Posting from frontend, get title from custom fields

Solved

Posting from frontend, get title from custom fields

  • I followed the instruction on how to set up posting from front end.

    It’s working except my site’s posts doesn’t really have titles and gets the title from the custom fields I set up in acf.

    Here’s my wp_insert_post code

    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
     
        // Create a new post
        $post = array(
            'post_status'  => 'publish' ,
            'post_title'  => get_post_meta($post->ID, "pickup_date", true). ' - ' .get_post_meta($post->ID, "leaving_from", true). ' to ' .get_post_meta($post->ID, "going_to", true),
            'post_type'  => 'rides' ,
        );  
     
        // insert the post
        $post_id = wp_insert_post( $post ); 
     
        // return the new ID
        return $post_id;
    }
     
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    I think this part is the problem:

    'post_title' => get_post_meta($post->ID, "pickup_date", true). ' - ' .get_post_meta($post->ID, "leaving_from", true). ' to ' .get_post_meta($post->ID, "going_to", true),

    it doesn’t work. it doesn’t get the custom fields. I also tried to use get_field and the_field. Nothing has work yet.

    Is there a way to make it work? Am I missing something?

    Thank you.

  • Hi @rozeh

    The code deos not work because you are trying to load data from a post which has not yet been saved!

    You need to look in the $_POST array to find the posted data

    Good luck!
    Cheers
    E

  • How can I do that? Sorry my php is limited. If you can show me the code. It will be a great help.

    Thank you very much.

    EDIT I’m not sure i you’ll read this but I guess it’s better than adding a new reply but I finally fixed it.

    You are correct in about $_POST variable, I actually tried to use it days ago but it didn’t work.. because I was doing it wrong – wit the field_key or input name.

    I tried using
    $_POST[fields[field_51fc91fcd78f0]]
    but didn’t work.

    Upon searching just now, I found a thread here with the correct way which is
    $_POST["fields"]['field_51fc91fcd78f0']

    And it works!

    Thank you! 🙂

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

The topic ‘Posting from frontend, get title from custom fields’ is closed to new replies.