Skip Navigation

[Resolved] Sorting by a field that isn't required… (some posts are missing)

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 4 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1777695

I have a view with two "order" parameters. One is a custom field called "featured" and the other is the post date. I want all posts to be displayed by default in this view (and the user can search/filter manually). "Featured" is not a field the end user has access to. (It's going to be a paid option.)

The problem is, when I create a new post, it doesn't show up in this view.

I did some searching, and found this support thread: https://toolset.com/forums/topic/view-data-not-showing-up/ where it was mentioned that "Since you are sorting based on that custom field, if the Reviews do not have a set Review Date, then they will not appear in the search results. It's a quirk in the way WordPress queries work."

Based on that, I'm assuming I have something similar going on, and the reason posts aren't appearing is that they have no data in the "featured" field by default (it's not in the form that creates the posts) and therefore they are left out of the view, based on the Order parameter.

I have found that if I check "Featured" and then un-check it, the post will start appearing in the view.

So that all makes sense, but it doesn't do what I need. The question is, how do I make featured posts show up first, while also having newly added posts show up? The "Featured" field stores a 1 (featured) or a 0 (not featured) or...I'm assuming, a nothing, when the post is first created. Can I get it to put a 0 in the Featured field when posts are created, instead of a nothing? Is there a different and better way to accomplish what I'm going for here? I can't just add the featured checkbox to the post form, because users are supposed to have to pay for featured.

What is the link to your site?
hidden link

#1778273

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

From your description this seems to be exactly the same case as you described that the value for the custom field "featured" is not get stored and you will require to set the default value for the "featured" field when you create a new post.

I would like to know first are you creating new post from backend (admin) or using the Toolset form?

If you are using Toolset form to create new post from the frontend, you can use the Toolset Form hook "cred_save_data" to set the default value for the field "featured".

Once you clarify first how you are adding posts, I will be able to guide you in the right direction.

#1778877

Cool, ok, yes I am creating these posts using a toolset form.

(It seems, actually, that making any update using the backend editor causes a value to be written to the featured field, fixing the problem on that particular post, but customers won't have access to the backend.)

The cred_save_data solution sounds like exactly what I need, if you could elaborate a bit on how that works, that would be awesome 🙂

#1778929

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So, Toolset forms offers the hook "cred_save_data" that will run after the post is created once you submit the form.

You can use the hook "cred_save_data" and update your featured custom field value:

For example :
you can add the following hook to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
Or
You can add it to functions.php file of your theme.

add_action('cred_save_data', 'func_update_featured_field',10,2);
function func_update_featured_field($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==9999) {
              update_post_meta($post_id, 'wpcf-featured', 2);
        
    }
}

Where:
- replace 9999 with your original Toolset form ID
- with update_post_meta() function, adjust the custom field slug (if required) and value to whatever value you want to save with that custom field by default

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data