Home › Forums › Front-end Issues › Multiple CPT front-end post issue
Elliot ACF is the amazing!
I’m having a little trouble with posting from the front end.
I have three different CPT all which I would like to have users post via front end. In my functions.php I have a different function for each one.
If the CPT “opportunity”, whoms functions comes before the the latter to functions in the functions.php then it gets post correctly, if I try to post
CPT “talent” an error Undefined index displays in the line that CPT “opportunity” post_title is.
Below is the code to my functions.php:
// opportunity frontend form post
function opportunity_post_frontend( $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' => $_POST["fields"]['field_527c9145cb6c9'] ,
'post_type' => 'opportunity' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'opportunity_post_frontend' );
// talent frontend form post
function talent_post_frontend( $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' => $_POST["fields"]['field_527d961821e25'] ,
'post_type' => 'need' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'talent_post_frontend' );
// event frontend form post
function event_post_frontend( $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' => $_POST["fields"]['field_527c844ac4ac6'] ,
'post_type' => 'event' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'event_post_frontend' );
to get an Idea of my front end form:
<?php
/**
* Template Name: Opportunity Post
*/
acf_form_head();
get_header();
the_post();
?>
<div id="content" class="clearfix row-fluid">
<div id="main" class="span12 clearfix" role="main">
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<?php
$options = array(
'post_id' => 'new', // post id to get field groups from and save data to
'field_groups' => array( 4 ), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Post Opportunity', // value for submit field
'updated_message' => 'Your opportunity has been posted!', // default updated message. Can be false to show no message
);
acf_form( $options );
?>
</article>
</div>
</div> <!-- end #content -->
<?php get_footer(); ?>
Once again ACF killer!
Hi @rvirvo
Thanks for the code.
Only 1 of your 3 functions will ever run. This is due to the line:
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
After you insert the first post, you update $post_id param.
Your 2 other functions then bail early due to the code I highlighted above.
Does this help to explain?
If not, can you please detail exactly what you need help with? What are you trying to do? What is and isn’t working? What is the final objective for the code to complete?
Thanks
E
I’m having trouble with this issue also. Could one of you provide me the solution to this issue? Maybe updating this tutorial with the solution to this problem might be a good thing to do?
Basically I have one form to create a new post for one custom post type (using the code provided in the tutorial) and one form to create a new post for another custom post type (also using the code provided in the tutorial). What I’m understanding is that, using the code provided in the tutorial, this will never work?
Hi @florushj
I don’t understand what your issue is. Can you please create a new topic and clearly explain what the issue is?
Thanks
E
Can’t create a new topic in the weekends, so a reply might be the best solution.
Basically I want to know how to make two seperate forms (on two different pages even) on the front end where users can create posts. Form one is used to create posts of the custom post type A. Form two is used to create posts of the custom post type B.
So far so good. But when creating posts from the front end forms they seem to use only the code from one of the functions. Form two also creates a post of post type A instead of B for example. Which probably has something to do with the answer you replied to @rvirvo .
You say they will never work this way because of the updated $post_id param. My issue, or my question if you will is: could you explain to me how (if possible) to make this work.
Thanks in advance, and I love your ACF plugin (and add-ons).
Hi @florushj
The issue here is that bother forms (created with the acf_form function) will trigger the same ‘acf/pre_save_post’ filter.
To allow for multiple pre_save_post filters and multiple forms, you need to find a way to connect the form to the correct filter! This can be done by customizing the ‘post_id’ param in the form.
Current you are using ‘new’ for the post_id, correct? If you canged that to ‘new_cpt_a’ on one form, and ‘new_cpt_b’ on another form, then you can run the correct filter like so:
// within filter 1
// check if this is to be a new post
if( $post_id != 'new_cpt_a' )
{
return $post_id;
}
// within filter 2
// check if this is to be a new post
if( $post_id != 'new_cpt_b' )
{
return $post_id;
}
Hope that helps
Thanks
E
The topic ‘Multiple CPT front-end post issue’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.