Skip Navigation

[Resolved] Custom Field Value (Radio Button) Triggers Post Status Change

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by aaronS-5 4 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1457217

I added a custom post status via functions.php for a custom post type (properties), the post status is "Off Market." (which removes it from being published). I have a custom field group on the backend to choose the status of the listing, its a radio group with multiple options. Is there a way to trigger the listing to the "off market" post status when choosing the "off market" radio choice in that custom field group when updating the post? Basically, adding an action that when hitting the update post button it would check that custom field value for "off market" and if so, change the post status.

Thanks!

#1458411

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

Thank you for getting in touch.

Would you mind letting me know the slug that you used for the post status as well as the slug for the custom field that is used to set the status?

We should be able to do this using the our Toolset Forms custom hook to modify the post status from this field.

Thanks,
Shane

#1458417

Great, thanks Shane.

The post _status slug is 'Off Market' on the 'properties' custom post type.

The custom field slug is 'listing-status' (radio field type) and the option that I want to trigger the Off Market field post status is 'Off Market.'

Let me know if you need any more information, thanks again!

#1458565

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

You can try using this function below.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        if (isset($_POST['listing-status']))
        {
            $my_post = array(
      'ID'           => $post_id//other means of ID,
      'post_status'   => $_POST['listing-status'],
  );
   
// Update the post into the database
  wp_update_post( $my_post );
        }
    }
}

Add this code to the custom codes section in Toolset -> Settings -> Custom code, change the 12 in the code to the ID of your form.

Once you have done this you can activate the code.

Please let me know if this helps.
Thanks,
Shane

#1458657

Shane, is this possible without using a cred form? Basically, using a function in the functions.php to check for the value when resaving / updating the listing in wp admin and then triggering the listing status.

Admins fill out the information / custom fields on the backend of the site and not through the use of a form because there are multiple content templates depending on the property that need to be selected.

Thanks!

#1460171

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

Yes this is possible.

Try using this.

add_action( 'save_post', 'update_post_status', 10,3 );
 
function update_post_status( $post_id, $post, $update ) {
    $status = types_render_field( 'listing-status'); 
   $my_post = array(
      'ID'           => $post_id//other means of ID,
      'post_status'   => $status,
 );
    
// Update the post into the database
  wp_update_post( $my_post );
}

Please let me know if this helps.
Thanks,
Shane

#1460405

Shane, having some issues inserting the code without getting a php error. I do have some actions already in my functions.php for 'save_post' relating to calculating custom fields upon update or saving of the post, could this be causing an issue?

add_action( 'save_post', 'func_calc_and_update_space', 99, 2 );

This is the code I'm using to register the custom post status in the functions.php that's currently working fine.

// Registering custom post status
function wpb_custom_post_status(){
    register_post_status('Off Market', array(
        'label'                     => _x( 'Off Market', 'properties' ),
        'public'                    => false,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Off Market <span class="count">(%s)</span>', 'Off Market <span class="count">(%s)</span>' ),
    ) );
}
add_action( 'init', 'wpb_custom_post_status' );
 
// Using jQuery to add it to post status dropdown
add_action('admin_footer-post.php', 'wpb_append_post_status_list');
function wpb_append_post_status_list(){
global $post;
$complete = '';
$label = '';
if($post->post_type == 'properties'){
if($post->post_status == 'Off Market'){
$complete = ' selected="selected"';
$label = '<span id="post-status-display"> Off Market</span>';
}
echo '
<script>
jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"Off Market\" '.$complete.'>Off Market</option>");
$(".misc-pub-section label").append("'.$label.'");
});
</script>
';
}
}
#1460487

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

Would you mind allowing me to have access to the site to test this out a bit as the code to set the status should work.

The private fields have ben enabled for your next response.

Thanks,
Shane

#1462521

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

Just gathering a few more information.

Is it that you only want the houses on the frontend to be displayed only when they are available ? Are you using a view to do this ?

If so then you can just add a query filter to the view so that it only displays posts that are available without having to modify the actual post status itself.

Please let me know.
Thanks,
Shane

#1462559

Hi Shane,

This is strictly a backend request. I have a query filter set up to only display properties that have a status of "available" and all the other statuses, including "off market" do not show up on the frontend on the archive view. Basically, this would remove the step on the backend to have to change the custom field to "off market" AND the post status to "off market." Essentially removing the double step, so thats why I wanted to see if when choosing "off market" on the custom field it would automatically trigger the listing post status "off market" on update.

The "off market" post status moves it to draft / review. The other choices in the "off market" radio custom field group are still published, so thats why I wanted to trigger the post status.

#1462623

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aaron,

Ok so i've written the hook for you and already implemented it on the site.

add_action( 'save_post', 'wpse_78351_status', 10, 2 );

function wpse_78351_status( $post_ID, $post )
{
	$status = types_render_field( "listing-status", array( "item" => $post_ID ));
    if ( 'properties' == $post->post_type ) //adjust the condition
    {
		
		if ( $status == 'Off Market' ){
        // Update post 37
        $my_post = array(
            'ID'           => $post_ID,
            'post_status'   => 'Off Market',
        );
 		        remove_action( 'save_post', 'wpse_78351_status' );
            wp_update_post( $my_post );
		        add_action( 'save_post', 'wpse_78351_status' );

    }
	}
}

This should help now.

Thanks,
Shane

#1464651

Great, thanks Shane! Looks like things are working well, appreciate the help as always.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.