Hi,
I have this view displaying a list hidden link
I would like to make some element first in the list, and all other to be sorted alphabetically.
In order to do this, I first added a new number field on the post type, then entered a "3" value on the post I want to feature.
Finally, I changed the sorting of the archive view in order to make a 2 steps sorting, first based on the post custom value, second on the post title.
But I noticed on frontend that all posts with no custom value aren't displayed. I was thinking they would be sorted considering the empty field as "0", but they don't.
How may I achieve what I want without having to edit all posts in database ?
Thank you.
That's how WordPress queries work: if you order by some field, and posts which have no value for that field will not be included in the results.
There's nothing Toolset can do about that, and in such circumstances I would normally say that you would need to run a script to update all of the posts with no value to set one, and then to make sure that all future posts have a value saved.
You can add support for sticky posts to your custom post type, so in the post edit screen you can mark some post as sticky, and then in the settings specify that it should apply that to the archive for that post type, so that it appears first when viewing the archive. That sounds exactly like what you are aiming to do, do you want to give that a try?
Hello and thank you,
This plugin will not be enough, since I need a numeric value in order to be able to sort those results. So I will edit all posts.
Also, I need to do the same on a children posts view, where I need to display children of featured parents at the top of the list.
I assume I can't base the sorting on a parent custom field post, right ? If so, then what would you suggest in order to achieve this ?
Thank you.
Parent post (featured) 1
child 1 of this post
child 2 of this post
child 3 of this post
Parent post (featured) 2
child 1 of this post
child 2 of this post
Parent post (featured) 3
child 1 of this post
child 2 of this post
child 3 of this post
child 4 of this post
Parent post (other)
Parent post (other)
Parent post (other)
Parent post (other)
Parent post (other)
Parent post (other)
Parent post (other)
Parent post (other)
post child of featured parent
post child of featured parent
post child of featured parent
post child of a not featured parent
post child of a not featured parent
post child of a not featured parent
post child of a not featured parent
post child of a not featured parent
So this is an archive of parent posts, but none of the parent posts are being output themselves, they are just a jumping off point to display their children?
Or this isn't an archive of parent posts at all, it's an archive of the child posts, but you want them ordered such that if the post has a parent with a featured-field, it will be displayed at the top?
This has implications about the possible solutions.
This isn't an archive of parent posts at all, it's an archive of the child posts.
But I want them ordered such that if the post has a parent with a featured-field, it will be displayed at the top.
Exactly.
Well, it is not possible to order one post type (child posts) by some field belonging to another post type (parent post).
This isn't really achievable without writing custom code.
My first instinct is that you would omit those posts from the archive and insert a separate View to display them at the top of the archive results.
But it's not straightforward to omit those posts from the archive, it can't be done by adding some Query Filter to the archive for the same reason that the archive can't be ordered by fields of the related parent. Nor would it be straightforward to create such a View.
If your archive doesn't need pagination then possibly the best option would be to use the native WP filter the_posts (https://developer.wordpress.org/reference/hooks/the_posts/) which would contain the results of the query which gets the child posts for the archive. Your task then would be to get the parents of each of these posts together with the featured custom field and use those to re-order the array of child posts as required.
If you are using pagination that won't work though, because some of the posts that should be featured may be buried in subsequent pages of results.
In that case you'd need a different approach, whereby you used pre_get_posts to modify the archive results to exclude the featured posts, and created a View to insert at the top of the archive which was limited to the featured posts (using the Views API filter wpv_filter_query).
It would be much simpler if you stored the featured field on the child posts directly, instead of on their parents, as then you would need no custom code and could order your archive first by the featured field and then by whatever was appropriate, e.g. by post date or title. (That would still require you to make sure all posts had some value for that field.)