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