0

I'm using ACF to make custom posts from the frontend. I created a page in my theme, with the code:

// generate post title on submit
function auto_title_insert( $value ) {
  $value = $_POST["fields"]['field_5481df76aed49'].'-'.$_POST["fields"]['field_5481df80aed4a'].'-'.$_POST["fields"]['field_5481df86aed4b'].'-'.$_POST["fields"]['field_5481e101af995'];
  return $value;
}
add_filter( 'title_save_pre', 'auto_title_insert' );

// update post title on update
function my_acf_update_value( $value, $post_id, $field ) {
    global $_POST;
    $new_title = $value;
    $new_slug = sanitize_title( $new_title );
    $my_post = array(
        'ID'         => $post_id,
        'post_title' => $new_title,
        'post_name'  => $new_slug,
        'post_type' => 'mycpt',
  );
  wp_update_post( $my_post );   
}
add_filter('acf/update_value', 'my_acf_update_value', 10, 3);

// other
add_action( 'wp_print_styles', 'custom_acf_deregister_styles', 100 );
function custom_acf_deregister_styles()
{
    if (! is_admin() )
    {
        wp_deregister_style( 'wp-admin' );
    }
}
acf_form_head();
get_header();
?>

    <div id="sidebar_layout" class="clearfix">
        <div class="sidebar_layout-inner">
            <div class="row grid-protection">

                <?php get_sidebar( 'left' ); ?>

                <!-- CONTENT (start) -->

                <div id="content" class="<?php echo themeblvd_get_column_class('content'); ?> clearfix" role="main">
                    <div class="inner">
                        <?php themeblvd_content_top(); ?>

                        <?php while ( have_posts() ) : the_post(); ?>
                            <?php get_template_part( 'content', themeblvd_get_part( 'page' ) ); ?>
                            <?php
                                $args = array(
                                    'post_id' => 'new',
                                    'field_groups' => array( 'acf_timesheet' ),
                                    'post_type' => 'tspg_timesheet',
                                    'post_title'  => $_POST["fields"]['field_5481df76aed49'].'-'.$_POST["fields"]['field_5481df80aed4a'].'-'.$_POST["fields"]['field_5481df86aed4b'].'-'.$_POST["fields"]['field_5481e101af995'],
                                );
                                acf_form( $args );
                            ?>



                            <?php themeblvd_page_footer(); ?>

                            <?php if ( themeblvd_supports( 'comments', 'pages' ) ) : ?>
                                <?php comments_template( '', true ); ?>
                            <?php endif; ?>

                        <?php endwhile; ?>

                        <?php themeblvd_content_bottom(); ?>
                    </div><!-- .inner (end) -->
                </div><!-- #content (end) -->

                <!-- CONTENT (end) -->

                <?php get_sidebar( 'right' ); ?>

            </div><!-- .grid-protection (end) -->
        </div><!-- .sidebar_layout-inner (end) -->
    </div><!-- .#sidebar_layout (end) -->

<?php get_footer(); ?>

For some reason the post title is not generating and the post is not getting created. What am I doing wrong?

I used the advice from this post: How to update a custom post title from a front-end form using ACF fields?

  • you can't have your filter functions inside the page template, by the time the template loads, those filters have already happened. put them in functions.php. – Milo Dec 28 '14 at 5:27
  • @Milo you're right, done. Still does not fix the post creation issue though. Any thoughts on why the custom post is not being created? – Rizzo Dec 28 '14 at 6:18
  • wp_update_post does not create a post; use wp_insert_post instead. Also, do you have a post type called "mycpt"??? I think you forget to change that with the correct post type. Other than those things, we can not check further because you are using a filter that is not from WordPress (add_filter('acf/update_value'...) and the support for it is off-topic here. – cybmeta Dec 28 '14 at 9:30
-3

If you want to add ACF form with content, by one click check out this plugin

| improve this answer | |
  • 2
    Please do not post links as answers. Post a solution and explain how to solve the issue – Pieter Goosen Jul 1 '15 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.