Skip Navigation

[Resolved] Sorting view results by parent custom field or parent post title

This thread is resolved. Here is a description of the problem and solution.

Problem:

Sort child posts by custom fields of parent post type.

Solution:

There isn't such kind of built-in feature within Toolset plugins, Toolset Blocks/Views plugins are using WordPress class WP_Query to query posts, see WP document:

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

‘meta_value‘ – Note that a ‘meta_key=keyname‘ must also be present in the query.

That means you can only sort the results by custom fields of post type(“Institutional position”) you are querying, you can not sort the result by custom fields of other post type(“People”).

As a workaround, you can try with a nested views, for example:

https://toolset.com/forums/topic/sorting-view-results-by-parent-custom-field-or-parent-post-title/#post-2358545

Relevant Documentation:

This support ticket is created 2 years, 8 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 11 replies, has 2 voices.

Last updated by Silvia 2 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2357193

Hello everyone!
I have two custom post types: one is “People” the other is “institutional Positions”. They are related and One person can have multiple Institutional positions.
The Cpt People has custom fields Last Name, First Name plus Featured Image. The Cpt “Institutional position” has a custom field “Institutional Position Type” that lists 10 different types of positions.
I need to have a view filtered by Position Type (Ie display all members of Board of directors) displaying People featured Image and People Last+First name ORDERING results by Parent custom field “Last Name” ascending (they are political positions, so hierarchy is important and the only “fair” sorting order is alphabetical order). Is there a way to achieve this?

#2358545

Hello,

There isn't such kind of built-in feature within Toolset plugins, Toolset Blocks/Views plugins are using WordPress class WP_Query to query posts, see WP document:
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
‘meta_value‘ – Note that a ‘meta_key=keyname‘ must also be present in the query.
That means you can only sort the results by custom fields of post type(“Institutional position”) you are querying, you can not sort the result by custom fields of other post type(“People”).

As a workaround, you can try with a nested views, for example:
1) Parent post view:
- Query posts of parent post type "People"
- Sort the result by custom field "Last Name"
- In view's loop, display below child post view

2) Child post view:
- Query posts of child post type "institutional Positions"
- Filter by relationship between “People” and “institutional Positions”
- In view's loop, display “institutional Positions” post information

#2358809
whichfilter.png
EXAMPLE.png

Hello Luo, thanks for your answer. I am trying to implement what you're suggesting, but I guess your infos are not that clear for me. First of all what do you mean when you say "In view's loop display below child post view"? Do I have to type the view shortcode in between the wpv-loop tags?
Also: "filter by relationship between people and Institutional positions" : when I have selected the post relationship I also have to choose a filter type: which one is it? (whichfilter.png)
Lastly, when you say "in view's loop, display “institutional Positions” post information" : In the INSTITUTIONAL POSITION cpt I only need to filter the type of position but I don't have any other fields to display. I enclosed an image of the result I would like to achieve: the type of position would be the title of the section in the page and the results would have to be featured image + name of the person (example.png)... is that possible with your suggestion?
Thank you in advance for your assistance.

#2359531

You can setup the child post view first, then you can insert the child post view shortcode into parent post view's loop directly
For screenshot
https://toolset.com/wp-content/uploads/2022/05/2358809-whichfilter.png
In your case, it should be the second option: The current post in the loop
In the child post view, you can display it's parent "People" information with "item" attribute, see our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/
If your current post is in a relationship acting as an intermediary post, you can use the value of @{relationship-slug}.parent to obtain data from the related parent.

#2362351

Hello,
I did as you instructed, but even tho in the child view I chose "bootstrap grid" with 4 columns, the result is a list of people instead of a grid. I have 3 records in the CPT and they are listed one after another in 1 column. Any suggestions?

#2364095

For the new issue:
the result is a list of people instead of a grid.

It seems your website does not load Bootstrap JS/CSS files, please try these:
Dashboard-> Toolset-> Settings-> General
in section "Bootstrap loading", enable option: Toolset should load Bootstrap 4, and test again

#2364189
Screenshot 2022-05-16 at 08.48.28.png

Hey Luo,
that's the first setting I adjust. I am using Kadence + Toolset, if you need access to the website to have a closer look, just let me know. Thanks for now.

#2364251

Please provide your website credentials in below private message box, also point out the problem page URLs

#2364339

Thanks for the details, I assume we are talking about this page:
hidden link

It is expected result, since you are using nested views, each row display one child post view.

Are you going to merge all results into one row?
If it is, you need to customize the parent/child post views with HTML codes, for example, display HTML row div tag(div class="row ") in parent post view, in child post view display only HTML grid tag(class="col-md-12")

Please let me know if you need assistance for it.

#2364385

Hey Luo,
i did as you instructed, but nothing changed.
I added the "<div class="row">" to the parent view.

The child view already had :

	<div class="container wpv-loop js-wpv-loop">
	<wpv-loop wrap="1" pad="true">
		[wpv-item index=1]
		<div class="row ">
			<div class="col-md-12">[wpv-post-body view_template="loop-item-in-mostra-direttivo-child"]</div>
		</div>
		[wpv-item index=other]
			<div class="col-md-12">[wpv-post-body view_template="loop-item-in-mostra-direttivo-child"]</div>
		[wpv-item index=pad]
			<div class="col-md-12"></div>
		[wpv-item index=pad-last]
			<div class="col-md-12"></div>
		</div>
	</wpv-loop>
	</div>

Since the code in the child view was the one above, I thought I only had to change 12 to 4 as I would like 3 entries per row (the final result should display >40 entries), but the results are definitely not what I was expecting... what am I doing wrong?

#2364811

I have done below modifications in your website:
1) Edit the parent view:
hidden link
in section "Loop Editor", change the codes as below:

...
	<div class="row">
      <wpv-loop>
        [wpv-view name="mostra-direttivo-child"]
      </wpv-loop>
	</div>
...

2) Edit the child post view:
hidden link
Change the codes as below:

...
	<wpv-loop>
			<div class="col-md-3">[wpv-post-body view_template="loop-item-in-mostra-direttivo-child"]</div>
	</wpv-loop>
...

Test it in frontend:
hidden link
It works fine, please check it.

You can follow Bootstrap document to choose CSS class name:
hidden link

#2365199

My issue is resolved now. Thank you!