Skip Navigation

[Resolved] Ordering Posts based on custom date field with Grand-parent relationship

This support ticket is created 5 years, 1 month 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 14 replies, has 2 voices.

Last updated by deepS 5 years ago.

Assisted by: Beda.

Author
Posts
#1364019

Hi,
I have a job website with Job as custom posts. There are two other custom posts- Places and States. Places are directly related to Jobs as One-to-Many and States are directly related to places as One-to-Many. So in this way, States are indirectly connected with Jobs in the Grand-parent child relationship.
I have tried to create a page that can display information about the 'Job' posts related to specific 'State/s'. One page is created using Toolset views hidden link. It contains 'Job' posts in the States like Assam, Meghalaya, Nagaland, etc.
Now the problem is- I have a custom field in the 'Job' posts called the 'Last date of application/Interview' containing the date field and I want to order the Job posts based on that. But I couldn't.
Please help.

#1364557

You can only order a list of Jobs by that date, if the date is directly associated with the Jobs (it is filled in on the posts "job").
It also will only work if the View, where you plan to order by, is actually listing those Jobs.
If that is the case all you need to do is add a orderby to the View by that custom field.

Since Types Date Fields store Timestamps, you will want to order by "number" (It should actually automatically set it to number, when you choose the date field as order by option)

Can you confirm this works for you as well?

If not, I will need to know what precisely you tried, and maybe get backend access to a staging site where I can see the setup (Views and Posts).
I can then check what is wrong with the Views

I enable private messaging just in case this will be required.

#1364601
Bildschirmfoto 2019-10-19 um 17.10.46.png

The Login URL you provided is a 404
I am only able to (discovered accidentally) access a login form at hidden link

I hope that's the right one (I can login there with the provided data and also can find the respective views/pages, so I assume yes)

I see that you nest a View that lists Jobs in a View that lists Places which in turn is nested in a View that lists States.

You will be inserting the "States" View somewhere on a page, which in turn then will display the nested Places and those, in turn the nested Jobs, which you want ordered by the Date field.

So you need to add the order by to the View that lists jobs (17996).

The order by works, you can see that by altering the asc/desc, but due to how the view is nested and displaying only one (first) item it seems to be messed up somehow.
This is because the parent Views order first, then the child View.
In the Child loop the order is correct, as far I see, but because the parent view orders by post dates, that has prevalence, and then, in those single loops, the nested will be ordered by the date field.

The output you will get is:

- States (only the few, as by ShortCode attribute ID), ordered by Post Date.
-- Places (only the ones connected to the above States in the loop), again ordered by Post Date
--- Jobs (only the ones connected to the above Places), this time ordered by the Date field.

So you will get some output like:
- Post Date 1
-- Post Date 1
--- Field Date 1
--- Field Date 2
-- Post Date 2
--- Field Date 1
--- Field Date 2
- Post Date 2

Etc.

You can see that by adding "here to here" strings in the parent views, and control the Date order WITHIN the begin and end of your "here to here" string (please see screenshot)
As you see (since you index only the first in the loop) only one date is output and it's correctly ordered, since there is no other date output in that very loop.
But if you put the date BEFORE your index one in the loop of the Jobs View you will see the ordering is correct.

Can I ask why you use index=1 and no index= other, in the Jobs View?
The current code will make it so that only the fist item will ever be addressed, but you will potentially have more than one job each place<>state, I assume?
I would not use index attributes that only index the first item, as all others would be not addressed in this case.

I have set you up with a whole duplicate example of your views, which are shown to be ordered by the date field you want, you can see this here
hidden link
It's using these Views: 18240, 18239, 18238

Please study the nesting and my Loops, so you can understand what I tried to explain above (and eventually adapt your current Views using the examples I made - or you can simply build on them)

Please let me know if I missed something.

#1364615

I hope I am getting your point. What I have understood from your works that you are ordering jobs as per the custom date field within the place. But I want them for the entire list irrespective of the place or state. How's that possible. Hope you got my point. Thanks

#1364625

Please have a look at the views I created:
They order the Jobs by the Field "Last date of application/Interview" - as you outlined to be the goal here https://toolset.com/forums/topic/ordering-posts-based-on-custom-date-field-with-grand-parent-relationship/#post-1364019
It does not do that in the Places View but in the Jobs View.

1. The most parent (grandparent) which is inserted in the page lists States ordered by Post Date (it could be anything else related to States)
2. The middle View which is inserted in the #1 Grandparent View lists Places, ordered by Post Date (again could be anything related to places)
3. The most child (the view with the Jobs) orders jobs by "Last date of application/Interview", correctly.

It is not possible to order all jobs chronologically related to each other if those are nested in other parent views, because each present view loop "resets" the ordering for the child view for each parent post, that is entirely expected.

It is a list within a list within a list, and you Can only order a list on the very list level, not from a nested level to a parent level or vice versa.
That is the same if you produce a simple HTML list, or a Custom PHP Loop.

If you need a View with Jobs that are all consecutively ordered (so not restarting for each place/state) then you need a View that lists Jobs directly, without nesting it into parent loops (because each parent loop will reset after being looped for each item).
That would be the same in PHP vanilla code.
You could never order something that is nested into another ordering, as a "global" order, but only within that loop.

On the other hand if you want to see only the Jobs for ONE State/Place, then you don't need the parent views either, you can then instead insert the Jobs View to a Place post, and tell it to show only the Jobs belonging to the current post (Place)
Then you can order them again by the field.

I hope it's clear! Otherwise please let me know where I lacked explanation 🙂

#1365185

You got me right that, I need a View with Jobs that are all consecutively ordered (so not restarting for each place/state). This view would serve my purpose of chronologically arranging the jobs as per the custom date field globally irrespective of Place/State. Please help me to achieve that.

#1366227

Then you need a view that lists those posts at once, not "stacked" by a parent view that lists places or states

There is simply no solution for this, neither in custom code or native WordPress query, because a loop will re-initiate each time it's triggered.
Even if you would somehow flag the posts for their position in a loop, if you have a custom search or pagination this will again be "wrong", because reset each time the search or pagination would trigger

If you need a chronologically arrangement of jobs as per the custom date field globally irrespective of Place/State, you simply should neglect to bring Place/States into the display logic

Please let me know if I should set up an example or so on the site - but nothing else is needed than creating a View, that lists all Jobs ordered by that field.
That will show all Jobs in that order.
If you want only jobs belonging to a certain place or state, you can still add a query for that, in the very View, for example "related to the post where this View is inserted"
You would then insert the view directly in the State or Place post, and still see the properly ordered Jobs.

But first creating a loop of states, then IN that a loop of jobs, will unavoidably reset your order.

#1366245

I need job posts belonging to a certain state, but I couldn't add a query to that. Can you please help me with that. For example, I need jobs related to the states Assam and Meghalaya. How to do that?

#1366247

You can only query by direct parents and their direct parents, so if your Job is associated with State only trhu places, you need to query the jobs by place, not by state.

That is a problem as you cannot do that without a nested view, I understand now why you use a nested View.

Do you have a chance to use a taxonomy instead of a post relationship for the State?
Then you can directly query the Job by state.

#1366283

That's why I am asking for help. I cannot replace the Post relationship with Taxonomy as we have already created a very big database with it. Anyways let me find some other way. Thanks.

#1366313

I understand, but I cannot change how this works.
This is how a loop works, even letting Toolset aside for a moment.

I can try to see if there is a possibility to somehow circumnavigate this, but it would require some tampering with the setup which I prefer (and suggest) not to do online.
Could you share a copy of the site eventually so I can try locally to see how I'd solve this problem and then update you with some possible solutions?

Online this will not only be difficult but of course also affect the live site's front end, which I would like to avoid.
I have enabled a private reply for the data to be shared according https://toolset.com/faq/provide-supporters-copy-site/

I cannot promise a solution, but I will look at possible ones and possible workarounds, from there we should then be able to somehow resolve this.

Just to be clear:
You have a Parent State, Child Place, Child Job, as you elaborated already, and you want to display (anywhere) the jobs of one STATE (regardless the place) ordered by a field in the Jobs post.
Correct?

==> I think it'll simply not be possible without having another reference on the job level or at least on the direct parent level, but I will do my best to find a solution!

#1367085

Hi, Thanks for your kind help. As my site size is pretty big, the duplicator plugin has been interrupted to create a copy of the site. Instead, I would provide you a staging copy of the site so that you can check whatever it's required. Please wait while I am preparing it for you. Thanks.

#1367147

I re-enabled a private reply, so you can then provide that data privately and securely.

If it's possible, provide exact links to where what should be shown how, so I can wrap my head around it quickly.

Note that I can't guarantee a solution, but I'll do my best to find one (or a workaround).

Thanks!

#1370583
Bildschirmfoto 2019-10-26 um 18.43.55.png

There is a htaccess login on top of the site.
I was able to bypass that with the FTP login data.

I see you use the Rank Math SEO plugin, which is currently incompatible with Toolset - We are working on this issue on other tickets.

Since the Jobs are connected to States only via Places you cannot display Jobs directly according States.
You will need to Display Places according States, then Jobs according Places.

There is no solution to this, unless you have the chance to connect Jobs directly to the States.
And even then, you will (since you want to display the Jobs of MANY states) either need a parent States View (which will effectively hinder you to order the Jobs independently) or a View that lists Jobs by some Parent States passed for example in a URL attribute or ShortCode attribute.
That would then allow to list those Jobs ordered consecutively by that field.

The only solution hence is a direct relation ship between the Jobs and States, if you want to order the Jobs consecutively by that field.
Otherwise, the solution that I provided initially is the way to go, which will however first order by the States, then Places, then Jobs.

Unfortunately there is no solution for this.

#1371869

Sorry to learn that you couldn't resolve my issue.