Skip to navigation Skip to content
OTGS Knowledge Base
  • Login
  • Home

Update Date Field with Today’s Date

This short CRED Code allows you to get the TODAY’s Date in seconds from Timestamp begin and update a Date Field (Types) with this value. Since Views can currently not handle post_date in Parametric Searches this can be useful to find posts by Publish Date (let’s say you updated the CF with TODAY date on post creation)

/** 
  *Update news post date to a CF since Views cant handle Post Date in param search
  */
        
    function update_post_date_save_data_action($post_id, $form_data){
            
        // Change your CRED Form "ID" accordingly below
        if ($form_data['id']==ID){
                
            // get current date, returns full Timestamp
            $post_date = date("U");
                
            //update the CF with current post date

            update_post_meta( $post_id, 'wpcf-date_field_slug', $post_date );       
        }
    }
add_action('cred_save_data', 'update_post_date_save_data_action',10,2);
Tags: front-end admin, update_post_meta

Assign different Layout depending on Post Status

This code allows you to assign a specific Layout to Posts in “draft” mode only. Read inline comments as well, the thread is interesting too.

function assign_layout_by_post_status( $id, $layout ){
 
        global $post;
 
        $preferred_layout = 5745; // my layout ID
 
        $control_layout = 0; //just set to 0 if you don't have any layout set for your post/product
 
        if( $id === $control_layout && $post->post_status === 'draft' && $post->post_type === 'product'){
            return $preferred_layout;
        }
        return $id;
    }
 
    add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);
Tags: front-end admin, update_post_meta

Copy value of child custom field to another, from Parent Post

Copying a value from a child posts custom field to another one, when saving the parent post in the Admin.

There is an action for when we hit Save All child on parent post page (Types).
It’s wpcf_relationship_save_child and has two arguments, $child and $parent. Both are objects.

add_action( 'wpcf_relationship_save_child', 'wpcf_date_relationship_save_child', 10, 2 );
 
function wpcf_date_relationship_save_child( $child, $parent ) {
    $date = get_post_meta($child->ID, 'wpcf-start-date', true);
    $month = date('Y', $date); 
    $year= date("M",$date);
 
    update_post_meta($child->ID, 'start_month', $month);
    update_post_meta($child->ID, 'start_year', $year);
}
Tags: legacy_types_api, post-relationship, update_post_meta

Update User Profile with CRED, with CheckBoxes

Let a user edit his existing WordPress Profile directly from the FrontEnd through a CPT Edit/Create CRED form.

Note that this function assumes a setup with the correct CPT’s, custom fields, user CF’s.

The Process of this example CRED Form will create a CPT “user Profile” entry , which can be used for front-end displaying user profiles (with a content template as example).
During this, it will get the CPT field values and update the USER fields with it.

This example is also useful to see how checkboxes (multi-options) are different then “normal” CF’s.

function user_save_data_action($post_id, $form_data) {
       
        if ($form_data['id']==ID) {
           
            //get curretn user's ID
            $user_id = get_current_user_id();
 
            //get Skype post_meta field value he submits in post
            //note: any other SINGLE option Field applies to below "rule")
            $skype = get_post_meta( $post_id, 'wpcf-skype', true );
            //update the user_meta field with this value
            update_user_meta($user_id, 'wpcf-skype', $skype);
 
            //repeats as above
            //again, ANY single value Field can be used (email, URL, etc)
            $phone = get_post_meta( $post_id, 'wpcf-phone-number', true );
            update_user_meta($user_id, 'wpcf-phone', $phone);
            $pic = get_post_meta( $post_id, 'wpcf-profile-photo', true );
            update_user_meta($user_id, 'wpcf-profile-image', $pic);
           
            //now the tricky part, we are handling with CheckBoxes, multiple Options!
            //AGAIN, THANK YOU +Ana Couto !!!!
 
            //Check if actually the post_meta Checkboxes field has value(s)
            if ( isset($_POST['wpcf-languages'])){
                //if yes, get all the user meta fields and custom fields information (of user + post)
                $langs_user = get_option( 'wpcf-usermeta' );//users fields
                $langs_fields = get_option( 'wpcf-fields' );//posts fields
                //refine this, we only want specific fields, actually the "languages" and "support-languages" fields
                //being those the checkboxes fields (with options), once for user, once for post
                $langs_user_options = $langs_user['support-languages']['data']['options'];//user field
                $langs_fields_options = $langs_fields['languages']['data']['options'];//post field
 
                //check the field's values against each other (user_meta vs post_meta field values)
                $langs_selected = $_POST['wpcf-languages'];//get values from post field (all of them)
 
                //define the data with which we are going to update user_meta field
                $user_langs = array();
                    foreach ($langs_selected as $fields_option_key) {
                        if (isset($langs_fields_options[$fields_option_key])) {
                            $value = $langs_fields_options[$fields_option_key]['set_value'];
                            foreach ($langs_user_options as $user_option_key => $user_option) {
                                if ($user_option['set_value'] == $value) {
                                    $user_langs[$user_option_key] = array($value);
                                    break;
                                }
                            }
                        }
                    }
 
                //update the user_meta field
                update_user_meta($user_id, 'wpcf-support-languages', $user_langs);
            }
           
             //Now it get's easy again :)
            //Update post_name (slug) with user_name (author)
            //we don't want author to enter a title for his profile, this should be done automatically
            $custom_title = wp_get_current_user();//get user, define where to get info from for new title
            $new_title = $custom_title->user_login;//get login name from user
 
            //collect data and define new title
            $my_post = array(
                        'ID'            => $post_id,
                        'post_name'     => $new_title,
                        'post_title'    => $new_title,//we NEED to do that, otherwise, AUTO DRAFT will be post name
 
            );
 
            // Update the post into the database
            wp_update_post( $my_post );
 
        }
    }
add_action('cred_save_data', 'user_save_data_action',10,2);
Tags: checkboxes, database, front-end admin, membership, update_post_meta

Update post title/name with a Date from a Types Date CF

Update post title/name with a Date taken from a types Date CF (and obviously you won’t 2664748859 as title but 11-06-2014 or similar) with CRED hook. Remember that Toolset uses Timestamp but you want a “normal” date as Post Title.

function ttd_save_data_action($post_id, $form_data){
            // Change your CRED Form "ID" accordingly below
            if ($form_data['id']==ID){
               
                //Declare the content of your variables, change "your_custom_field_slug" accordingly
                $custom_value = get_post_meta( $post_id, 'wpcf-your_custom_field_slug', true );//get posts' Date field value (timestamp)
                $custom_title = gmdate("m-d-Y", $custom_value);//convert timestamp to m-d-y for output
                //collect data and define new title
                $my_post = array(
                    'ID'               => $post_id,
                    'post_title'   => $custom_title,
                    'post_name' => $custom_title,
                   
                );
               
                // Update the post into the database
                wp_update_post( $my_post );
               
            }
        }
    add_action('cred_save_data', 'ttd_save_data_action',10,2);
Tags: database, front-end admin, get_post_meta, update_post_meta
OnTheGoSystems - One team, one goal

Report Post

« »

 

Your Name:

Your Email:

Please tell us why do you think this post is inappropriate and shouldn't be there:


Cancel Report