3

I am using A custom Post Type with Advanced Custom Fields to create custom entries of a post. My Customs Post Type and Options are added with (please scroll on codebox to see all):

add_action( 'init', 'register_cpt_campaigns' );

function register_cpt_campaigns() {

$labels = array(
    'name' => __( 'Campaigns', 'campaigns' ),
    'singular_name' => __( 'campaign', 'campaigns' ),
    'add_new' => __( 'Add New', 'campaigns' ),
    'add_new_item' => __( 'Add New Campaign', 'campaigns' ),
    'edit_item' => __( 'Edit Campaign', 'campaigns' ),
    'new_item' => __( 'New Campaign', 'campaigns' ),
    'view_item' => __( 'View Campaign', 'campaigns' ),
    'search_items' => __( 'Search Campaigns', 'campaigns' ),
    'not_found' => __( 'Campaign not found', 'campaigns' ),
    'not_found_in_trash' => __( 'Campaign not found', 'campaigns' ),
    'parent_item_colon' => __( 'Parent campaign:', 'campaigns' ),
    'menu_name' => __( 'Campaigns', 'campaigns' ),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => false,
    'description' => 'Custom post type for Discovr Campaigns',
    'supports' => array( 'author','title' ),
    'taxonomies' => array( 'campaign_category', 'campaign_action' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'menu_icon' => 'dashicons-welcome-widgets-menus',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => false,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);

register_post_type( 'campaigns', $args );
}

And custom Taxonomies with (please scroll on codebox to see all):

// Register Campaign Type Taxonomy
function campaign_post_category() {

$labels = array(
    'name'                       => _x( 'Campaign Types', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Campaign Type', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Campaign Types', 'text_domain' ),
    'all_items'                  => __( 'All types', 'text_domain' ),
    'parent_item'                => __( 'Parent types', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent types:', 'text_domain' ),
    'new_item_name'              => __( 'New Campaign Type', 'text_domain' ),
    'add_new_item'               => __( 'Add New Campaign Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Campaign Type', 'text_domain' ),
    'update_item'                => __( 'Update Campaign Type', 'text_domain' ),
    'view_item'                  => __( 'View Campaign Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate campaign types with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove campaign types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular campaign types', 'text_domain' ),
    'search_items'               => __( 'Search campaign type', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No campaign types', 'text_domain' ),
    'items_list'                 => __( 'Campaign type list', 'text_domain' ),
    'items_list_navigation'      => __( 'Campaign type list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => false,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => false,
    'show_tagcloud'              => false,
);
register_taxonomy( 'campaign_category', array( 'campaign_categories' ), $args );

}
add_action( 'init', 'campaign_post_category', 0 );

// Register Campaign Status Taxonomy
function campaign_post_action() {

$labels = array(
    'name'                       => _x( 'Campaign Status', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Campaign Status', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Campaign Status', 'text_domain' ),
    'all_items'                  => __( 'All Status', 'text_domain' ),
    'parent_item'                => __( 'Parent Status', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Status:', 'text_domain' ),
    'new_item_name'              => __( 'New Campaign Status', 'text_domain' ),
    'add_new_item'               => __( 'Add New Campaign Status', 'text_domain' ),
    'edit_item'                  => __( 'Edit Campaign Status', 'text_domain' ),
    'update_item'                => __( 'Update Campaign Status', 'text_domain' ),
    'view_item'                  => __( 'View Campaign Status', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate campaign status with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove campaign status', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular campaign status', 'text_domain' ),
    'search_items'               => __( 'Search campaign status', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No campaign status', 'text_domain' ),
    'items_list'                 => __( 'campaign status list', 'text_domain' ),
    'items_list_navigation'      => __( 'campaign status list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => false,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => false,
    'show_tagcloud'              => false,
);
register_taxonomy( 'campaign_action', array( 'campaign_actions' ), $args );

}

add_action( 'init', 'campaign_post_action', 0 );

I also have some functions that are added to the theme to support my functionality of the post-type. I have removed these to rectify my issue but did not fix anything.

// Add Default Campaign Status
function set_default_campaign_status( $post_id, $post ) {
    if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
        $defaults = array(
            'campaign_action' => array( 'Draft' )
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'set_default_campaign_status', 0, 2 );

// Add Default Campaign Type

function set_default_campaign_type( $post_id, $post ) {
    if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
        $defaults = array(
            'campaign_category' => array( 'Not Selected' )
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'set_default_campaign_type', 0, 2 );

// Delete post

function delete_post(){
global $post;
$deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
if (current_user_can('edit_post', $post->ID)) {
    echo       '<a href="'.$deletepostlink.'" id=""><button class="m-t-10 m-b-10 btn btn-danger btn-cons"  type="button">Delete</button></a>';
}
}

//Redirect after delete post in frontend

add_action('trashed_post','trash_redirection_frontend');

function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
    wp_redirect( get_option('siteurl').'/campaigns' );
    exit;
}
}
// Add default Campaign Pages
function discovr_campaign_endpoints() {
add_rewrite_endpoint( 'overview', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-details', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-audience', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-page', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-ads', EP_PERMALINK );
add_rewrite_endpoint( 'edit-campaign', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
}
add_action( 'init', 'discovr_campaign_endpoints' );


// Update Campaign Page on Save
add_action( 'save_post', 'wpse105926_save_post_callback' );

function wpse105926_save_post_callback( $post_id ) {
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {

    // unhook this function to prevent infinite looping
    remove_action( 'save_post', 'wpse105926_save_post_callback' );

    // update the post slug
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => '' // do your thing here
    ));

    // re-hook this function
    add_action( 'save_post', 'wpse105926_save_post_callback' );

}
}
// Add Campaign Title Label and Instuctions
function my_acf_prepare_field( $field ) {

$field['label'] = "Campaign Title";
$field['instructions'] = "Internal use only. Ex: `Retarget Abandoned Bookings`";

return $field;

}
add_filter('acf/prepare_field/name=_post_title', 'my_acf_prepare_field');

Unfortunately, everytime I save a custom field it reverts to empty or as it's original. This only happens with the ACF fields and not with fields such as title, author or taxonomies not overwritten with ACF. Please see the following gif images to understand further.

Backend of Wordpress with Post being created and updated Backend of post being updated

ACF Custom Fields Group Settings ACF Custom Fields Group Settings

Frontend Post being created Frontend Post being created

I have tried removing all frontend funtionality but still doesn't work in the backend, also removed all plugins either than ACF. No error in debug or firebug. I can only imaging that there is a conflict in the fields, or ACF is not able to find the appropriate place to save these.

Any help is greatly appreciated and I hope that there is something simple that I have missed.

UPDATE

This works by changing the field_name and field_label to something obscure.

ACF Field Group working ACF Field Group working

Frontend form saving Frontend form saving

Getting this to now work is great, however, I need the field_label and field_name to match the purpose of these fields. I could overwrite these within the functions.php but this will be a nightmare as we potentially have 20 fields to add.

Darren
  • 2,176
  • 9
  • 42
  • 98
  • put custom fields in support parameter and than try. 'supports' => array( 'author','title' ,'custom-fields'), – Akshay Shah Sep 07 '17 at 05:45
  • Thank you for your suggestion Akshay. Unfortunately, that doesn't work. – Darren Sep 07 '17 at 05:52
  • What is totally bizarre is I have got this to work now by making the field `field_label` obscure like `asdasdasd` and the `field_name` like `asdasdasd` However any changes to these that use real words and it is broken again? – Darren Sep 07 '17 at 06:21
  • what changes you did recently? – Akshay Shah Sep 07 '17 at 06:32
  • I have just updated the post to show these – Darren Sep 07 '17 at 06:38
  • I got the reason your previous field name was set for any other post due to that it is not storing the data. Some time we use same key name or variable name multiple times so their values are get override and functionality not behaving proper. i guess with this your problem is solved. – Akshay Shah Sep 07 '17 at 06:47
  • I understand that and was my immediate thought that there was a conflict of field names. However, I have just updated the Label to be WHAT IS THE PURPOSE OF THIS CAMPAIGN? and this stops the field from saving. There is no way that this could be used anywhere else. I switch it back to `khkjhkjhk` and it works again. Total loss of the reasoning. – Darren Sep 07 '17 at 06:53
  • Let me post answer so other can take reference mark it as correct – Akshay Shah Sep 07 '17 at 07:00

2 Answers2

0

If you change the field_label to what is the purpose of this campaign and the field_name to what_is_the_purpose_of_this_campaign it does not work.

However, if you change the field_label` to whatisthepurposeofthiscampaign and the field_name to whatisthepurposeofthiscampaign it does work.

It seems that any spaces break the connection of the field_label and field_name

Akshay Shah
  • 3,391
  • 2
  • 20
  • 33
  • Thank you for your answer. This was my immediate thought that there was a conflict of field names. However, I have just updated the Label to be WHAT IS THE PURPOSE OF THIS CAMPAIGN? and this stops the field from saving. There is no way that this could be used anywhere else. I switch it back to khkjhkjhk and it works again. They only work when adding a jumble of letters. Total loss of the reasoning. – Darren Sep 07 '17 at 07:03
  • 1
    If I change the `field_label` to `what is the purpose of this campaign` and the `field_name` to `what_is_the_purpose_of_this_campaign` it does not work. However, if I change the field_label` to `whatisthepurposeofthiscampaign` and the `field_name` to `whatisthepurposeofthiscampaign` it does work. It seems that any spaces break the connection of the `field_label` and `field_name` – Darren Sep 07 '17 at 07:09
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Sep 07 '17 at 11:48
  • I have set your comment as a answer as i have given a hint but actually understanding is your. Thanks @TobySpeight – Akshay Shah Sep 07 '17 at 11:54
0

This error was created by permissions problems with certain plugins. I had uploaded via FTP as root, whereas Wordpress was using www-data as the file owner. I matched the permissions through the console and this has been fixed.

Thank you for your help.

Darren
  • 2,176
  • 9
  • 42
  • 98