Skip Navigation

[Resolved] Sorting by intermediary post type fields does not work

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.

Our next available supporter will start replying to tickets in about 1.77 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 19 replies, has 2 voices.

Last updated by Nigel 3 months, 4 weeks ago.

Assisted by: Nigel.

Author
Posts
#2707617
Screenshot 2024-07-13 004110.png
Screenshot 2024-07-13 004031.png
Screenshot 2024-07-13 003948.png
Screenshot 2024-07-13 003801.png
Screenshot 2024-07-13 003719.png
Screenshot 2024-07-13 003656.png

Tell us what you are trying to do?
I'm trying to sort the output of a view (a grid of employees = "Mitarbeiter" in German) by an associated intermediary post type custom field. But it only works with "normal" fields of my custom post type. When I set the ordering to my "Position für Sortierung" field the view has no output.

Is there any documentation that you are following?
Of course, my setup is advanced and I'm following the Toolset documentation.

Is there a similar example that we can see?
-

What is the link to your site?
hidden link - at "Unser Team"

#2707802

Nigel
Supporter

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

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

Hi there

I tested this on my local site, where it appears to work as expected.

I tested first with the legacy Views editor, then with the current block editor UI, and in both cases the output of the View was sorted by values of a custom field, which belongs to the intermediate posts of a relationship between two post types. The View is set to query the intermediate fields directly.

It looks like you are aiming to do something similar, but in your case it is not working.

Would it be possible to get access to your site so that we can take a closer look.

Let me mark your next reply as private so that we can get log-in credentials from you—you may want to create a temporary admin user for us to use that you can later delete. And be sure to have a current backup of your site.

#2708305

Nigel
Supporter

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

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

Hi Harry

If you want to order the output of the View by the value of a relationship field then the View must query the intermediate post type that stores the field values (you can only order something by properties of that thing).

I didn't quite understand your setup, as it looks like you added a View to output related posts to an individual Klinik post (rather than the template for Klinik posts to be used by all such posts).

Also, you are making the View with the block editor, but not using the block editor for pages etc.

So I made a demonstration by creating a free-standing Content Template ("Support") not assigned to any post types, so that I could add a View with the block editor, and then inserted that template into a page ("Support") to display it.

Please review it.

It queries the intermediate post type, but you will see that it outputs content (the titles) of the posts that are connected; the output is ordered by the value of the relationship field, handled as a number.

It appears to be working, if you could please verify.

Seeing it in action you can hopefully adopt a similar solution with your own Views.

#2708463

Hi Nigel,

thanks for your Demo on the site. Unfortunately, I can't adapt it to my usecase. I implemented your view on this page (don't want the domain to show up):
/wp-admin/post.php?post=756&action=edit
Now, I only want to display employees, which are related to that page (or rather clinic). Filtering your view in that regard did not work (no items found) so I reverted to your settings. Could you please help me further?

Thx
Harry

#2708479

Nigel
Supporter

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

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

I took another look and I'm a little puzzled that it's not working, it is a similar set up as my own test site where it is working.

Would it be okay to take a copy of your site to install locally where I can dig a little deeper?

#2708639

Yes, of course, you can grab a copy. Thanks in advance, I hope you can find a way to get this going!

#2708675

Nigel
Supporter

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

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

Screenshot 2024-07-22 at 09.02.03.png

I hadn't noticed until looking more closely at the copy I had installed locally, but for the post where you are displaying this View (post ID 756), the connected posts have no value for the relationship field used for sorting (Position für Sortierung), see the screenshot.

The way queries in WordPress work is that if posts are sorted by some field, if a post has no value for that field then the post is not included in the result. None of the related post connections have a value for that field, so no results are shown.

As soon as I provided a value for one of the connections, then it shows up in the results (I did that on your site).

#2708700
Screenshot 2024-07-22 123340.png
Screenshot 2024-07-22 122042.png

I also checked that those values were provided, I don't quite understand. It probably was another clinic, sorry about that.

I adapted my view now (Content Selection => Kliniken Mitarbeiter Intermediary Posts) and there are results incoming, which is good. But now my content template is wrong (of course). Is there a way to get the data, like image, title, etc. of the Mitarbeiter-innen Post Type instead of the Kliniken-Mitarbeiter Intermediary Post Type from inside a content template? And you can see inside the content template, that there is a dependent View selecting related clinics. That one stopped working as well.
This is my "real" view for the Mitarbeiter: /wp-admin/post.php?post=355&action=edit
And this is the content template: /wp-admin/post.php?post=247&action=edit

It should look like this: https://toolset.com/wp-content/uploads/2024/07/2707617-Screenshot_2024_07_13_004031.png

#2709153

Nigel
Supporter

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

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

Screenshot 2024-07-24 at 08.36.24.png

OK, I think I understand what the problem is.

On a Kliniken post (ID 355) you have the View (ID 372) which is now set to query the intermediate post type, and the output of that View includes an unassigned Content Template (ID 247).

The problem is, when editing the template and setting dynamic sources for blocks, you cannot specify that the source should be either the parent Kliniken post or Mitarbeiter post, and so it outputs, for example, the title of the intermediate post.

You need to set the context, by choosing a preview post (at the top) which is an intermediate post, and as the intermediate post has parents it would be possible to specify them as the source for dynamic sources.

But it is not possible to choose an intermediate post for the preview.

I suspect this particular scenario wasn't envisaged by the developers and they are excluded by default, I'm not sure.

In any case, while editing the template you can remedy the problem by temporarily assigning the Content Template to the intermediate post type. Refresh the page, and then choose an intermediate post as the preview post.

You can see in the screenshot I've done that, and now it is possible to specify the parents as the post source.

That was the issue, yes?

#2709221
Screenshot 2024-07-24 120608.png

Thanks, we're getting there, the correct Mitarbeiter title is now showing up. Do you have another trick for the Featured Image of a related Mitarbeiter? I can't choose it as a data source here (see screenshot) /wp-admin/post.php?post=247&action=edit.

I also played around a bit with the preview, but couldn't get it working. Also, the (missing) image (data source) seems to crash the whole template and subsequently also the rendering of the whole page where the View with this Content Template is shown.

The content template is now only for the Intermediary Post Type. I used it in other places as well before, but that doesn't work now anymore because the context changed.

#2709234

PS: When editing the content template - as soon as I check the checkbox "Kliniken Mitarbeiter Intermediary Posts" under Usage the content template and the view are broken as I described above. Unchecking doesn't resolve that problem either. Had to manually recover in Code Editor. Also the view showed empty Mitarbeiter. After investigating I found out that trashed Mitarbeiter were also pulled, but shown as empty items in the view.

#2709302

Nigel
Supporter

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

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

I went back to your site to confirm the issue with the source for the featured image, and in setting the template to be assigned to the intermediate post type it broke the template.

I don't know why that happened, because it didn't happen when I first did it, as described in my previous reply.

I've looked in the settings in the database and I can't see what would cause the problem with the broken template; if you were able to fix it before via the code editor, hopefully you can again. Sorry about that.

Rather than risk doing anything else on your site I created a sandbox where we can test this, but I didn't run into any problems, and everything worked as expected (with the proviso that the template must be assigned to the intermediate post type for its fields to be available in dynamic sources).

Here is a link to log in: hidden link

I made a relationship between post types Left and Right, each of which supports Featured Images, and there is a relationship field that stores the sort order.

I created a Content Template ("Intermediates") that outputs a mix of fields from the intermediate post type or one of the related post types.

I created a View directly in the Left post "My Left Foot" to query the intermediate posts, ordered by the ordering field, and inserted the template in the output of this View.

If you familiarise yourself with the set up and then visit the My Left Foot post on the front end you will see that it works, and I don't think you will encounter any problems editing the View or the template.

One observation about your statement: I used it in other places as well before, but that doesn't work now anymore because the context changed.

That is expected. A simpler template that only outputs fields from the "current post" would work in many different contexts, but a template that outputs fields from related posts where it matters what the origin post is can only really be used when displaying posts of that type.

In that sense, there is probably no reason not to assign your template to the intermediate post type (you won't ever be displaying these directly).

The challenge then, is to try and identify why this set up is failing on your site.

Might I suggest that you try editing the sandbox site and make changes to the template comparable to what you have in your own template, and see if anything breaks and take note of what causes it when it happens?

#2709371

Hey Nigel,

sorry for the hassle you got with this. I dug into your setup and apart from directly building your view in the My Left Foot post in the frontend instead of using a shortcode I can't see much of a difference. I also experimented with conditional blocks, they worked fine on your setup.

Now I tried rebuilding my stuff from scratch, but something is amiss and by now I'm pretty desperate. On my end, I can't get the Featured Images to show up in the dynamic data source of an image, nor in a conditional block. Even when building a new content template or building a new view in the context of my intermediary post type. I don't know what's wrong 🙁

Not even in your new "test" content template, I only ever get this selection:
https://toolset.com/wp-content/uploads/2024/07/2709221-Screenshot_2024_07_24_120608.png

Could you try here to get a featured image in?
/wp-admin/post.php?post=3777&action=edit

#2709692
Screenshot 2024-07-26 200307.png

Addendum: I switched to code editor and adapted the code from your site. Everything is the same, but it is still not loading the Featured Image. See the attached image.

Could it be a bug in the block editor? Is it possible to create a Content Template without Gutenberg? Do we even need the content template?

#2710222
Screenshot 2024-07-27 155026.png
Screenshot 2024-07-27 153347.png
Screenshot 2024-07-27 153130.png

I did some more testing and deactivated all plugins except Toolset and switched to the Twenty Twenty Four Theme. I still can't select the Featured Image as dynamic image source (see attachment). Lastly, I also tried it with another block - the default image block. Can't choose the featured image there either. I also checked the Toolset options with your test installation and made them the same (not many differences there). I did the same with our intermediary post types Left / Right and Kliniken / Mitarbeiter.

Maybe my intermediary post type is broken? Somehow WP can't seem to query or identify the Featured Image to be a source for an image.