Skip Navigation

[Resolved] Featured posts in archive view

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.

This topic contains 8 replies, has 2 voices.

Last updated by romanB-3 1 year, 1 month ago.

Assigned support staff: Nigel.

Author
Posts
#1772487

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.

#1773027

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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.

However, for your use case I wonder if you might not just use this plugin: https://wordpress.org/plugins/sticky-posts-switch/

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?

#1773065

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.

#1773167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Is this the kind of layout you are talking about?

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)
#1773171

It would be more like this :

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
#1773173

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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.

#1773191

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.

#1773247

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK.

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.

Whatever solution you adopt, you'll need to use the Post Relationships API to get the parent of a give child post so that the custom field can be tested, specifically toolset_get_related_post (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post).

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

#1773297

Thank you very much for this complete and accurate answer.