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.
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.
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.
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?
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.
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.