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


Posting from frontend, get title from custom fields

    • rozeh

    • August 14, 2013 at 1:11 pm

    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.

    • Elliot

    • August 14, 2013 at 2:37 pm

    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!

    • rozeh

    • August 14, 2013 at 2:42 pm

    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
    but didn’t work.

    Upon searching just now, I found a thread here with the correct way which is

    And it works!

    Thank you! πŸ™‚

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.