Skip Navigation

[Resolved] How to create a sticky post with CRED

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use CRED to create a post. In that CRED form, I would like to include a checkbox that the user can check to make this a "sticky" post. If the checkbox is not checked, the post should not be "sticky".

Solution:
You need to add a generic "checkbox" field to this form. Give it a name like "stick-post" and save the value "1" when it's checked. See the attached screenshot.

Then, update this code to work with this new checkbox field's slug and form ID:

add_action('cred_save_data', 'custom_set_sticky_post',10,2);
function custom_set_sticky_post($post_id, $form_data)
{
    //update this with your form ID
    if ($form_data['id']==12345)
    {
        if (isset($_POST['stick-post']) && $_POST['stick-post']==1)
        {
            stick_post($post_id);
        }
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/stick_post/
https://developer.wordpress.org/reference/functions/unstick_post/

This support ticket is created 6 years, 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by johanL-2 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#601729

Hi,
I'm would like to be able to decide if a post (not a custom post) is a sticky post or not. I would like to have that option in CRED form. I have tried to add the function mentioned in https://toolset.com/forums/topic/how-to-add-sticky-post-with-cred/ to my child theme functions.php but what should I do next? I don't understand how to proceed. I'm using Oceans theme
BR
Johan

#601813
Screen Shot 2017-12-27 at 11.55.54 AM.png

You need to add a generic "checkbox" field to this form. Give it a name like "stick-post" and save the value "1" when it's checked. See the attached screenshot.

Then, update your code to work with this new checkbox field's slug.

if (isset($_POST['stick-post']) && $_POST['stick-post']==1)

Finally make sure you have set the correct CRED form ID here:

//update this with your form ID
if ($form_data['id']==12)

That should give you the ability to make a new post sticky. If you create another CRED form that edits this post, you will need additional custom code to determine whether or not the sticky checkbox should be checked when the form loads. You will also need additional custom code to "unstick" a previously sticky post if the User unchecks the sticky box.

#602054

Brilliant now it is working
thanks a lot
Johan

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