Skip Navigation

[Resolved] Custom post order in a view

This support ticket is created 2 years, 4 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/Karachi (GMT+05:00)

This topic contains 13 replies, has 2 voices.

Last updated by szymonF 2 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2214573

My site contains a large number of records of texts, which are displayed using views and archives. I want to create several 'reading group blueprints' (custom post type) which will feature a selection of texts (posts). Think of it as featured product lists.

I created a many-to-many relationship between Posts and Blueprints and added a view to each Blueprint which displays the related posts. The problem is in arranging the posts within each Blueprint in the order I want. I understand that I can change the published dates of the Posts to engineer the right order, I can even set up a custom field and order by that. However, I want to future-proof this - I expect that as the number of Blueprints grows, I will have some cross-over as some Posts will appear on many Blueprints. Naturally, they will appear in a different order. So, what I really need, is a way to custom-order the posts on each Blueprint.

I read through some threads and saw that this might be complicated. But the specific issues people had were a bit different, so I'm posting this if there might be a solution in my case.

As an alternative, I wouldn't even mind completely foregoing the relationships and simply embedding the specific Posts directly in the Blueprint body - there will only be some 10 per Blueprint, so it's not a massive pain to do it by hand. Is there a good way to do it so that the embedded Posts will display using the content template they would display with if they were a part of a View loop?

#2215799

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

If you'd like to keep the ability to 'search posts by blueprints' or 'search blueprints by posts', then you should keep the many-to-many relationship and add an "Order Number" field into the intermediary post type of this relationship.

Since this field value will be stored with respect to each relationship connection between a post and a blueprint, you'll have the ability to order the display of the related posts or blueprints, independently.
( which means that you'll be able to control the custom order of the posts on each blueprint )

On the other hand, if the 'search by related posts' is not important and you just want to show the related posts in a specific order, then yes, you can drop the use of a many-to-many relationship and instead just add a single line type custom field "Related Post IDs" with the blueprint posts. In this field, you can add the post IDs of the related posts, in the desired order in comma-separated format ( e.g. 45,28,35,76,15 ).

Next, you'll create a view that shows all posts and then limit its results, based on the list coming from that custom field "related-post-ids", using the "wpv_filter_query" filter:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

For example, suppose that the target view's ID is "12345" and the custom field that holds the related post IDs has the slug "related-post-ids", then the code for the filter will look like this:


add_filter( 'wpv_filter_query', 'filter_posts_show_related_fn', 1000 , 3 );
function filter_posts_show_related_fn( $query_args, $view_settings ) {
	// process if only specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		// get the related post IDs from the custom field
		$related_post_ids = types_render_field( "related-post-ids", array( ) );
		// if some related post IDs exist, set them in the view's query
		if(!empty($related_post_ids)) {
			$related_post_ids_arr = explode(',', $related_post_ids);
			if(!empty($related_post_ids_arr)) {
				$query_args['post__in'] = $related_post_ids_arr; 
				$query_args['orderby'] = 'post__in'; 
			}
		}
	}
	return $query_args;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2220047

Hello,

Thank you for the detailed reply! I think the first option will be good for me, but I still need a little help with it.

I created a custom number field slug blueprint-order. I assigned numbers to related posts within a Blueprint. I then went to the view which displays the related posts and selected Order by field blueprint-order. But when I do that, the Blueprint doesn't display any related posts, it just says 'no items found'. Meanwhile, if I set the Order by back to by post date, it does again find the posts, just displays them in the date order.

What am I doing wrong?

#2222003

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Can you please share temporary admin login details, along with the link to the page where this view can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2223205

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the admin access.

I noticed that you've added a view "display related posts" that shows the "Posts". But, in the approach that I suggested you'll not show "Posts", but the intermediary post type "Blueprint Texts" that was created as a result of post-relationship "Blueprints" and "Posts".

Please go to the view's edit screen and set this "Blueprint Texts Intermediary Posts" post in the content selection setting, instead of the "Posts".

This will make sure that the intermediary posts are shown ordered by the "Blueprint Order" field.

The current post's scope in the view's loop item will change to intermediary post type, so if you'll use any shortcode to show the current post data, for example, 'wpv-post-title', it will show the title of the intermediary post:


[wpv-post-title]

To show the title of the related post, you'll need to add item attribute:


[wpv-post-title item="@blueprint-text.child"]

And to show the title of the related blueprint post:


[wpv-post-title item="@blueprint-text.parent"]

You'll find information about the 'item' attribute at:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

#2223513

Thanks! I tried this and it doesn't seem to work though. I changed the View to target the Intermediary Posts and I set up the content template to only display [wpv-post-title item="@blueprint-text.child"]. Alas, it only displays 'No items found'. Am I missing something?

A further question is - what is the syntax to target the child item in conditional clauses? For example, I have:
[wpv-conditional if="( $(wpcf-link) ne '' )"]display link[/wpv-conditional]

Will that also be

[wpv-conditional if="( $(wpcf-link) ne '' )" item="@blueprint-text.child"]display link[/wpv-conditional]

#2224083

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I've made a few changes to the settings of the view "display related posts" and the ordering is working now:

1. In the query filter settings, I selected the correct relationship name and the "The post where this View is shown" option.
( screenshot: hidden link )

2. Removed the orderby="blueprint-order" order="asc" attributes from the view's shortcode, used in the content template "Loop-Blueprint":


[wpv-view name="display-related-posts"]

The $ method in conditional statements, works only for the fields attached to the post type that is being shown by the view, which in this case is the intermediary post type "Blueprint Texts Intermediary Posts".

For using fields from the child or parent, you can use the "wpv-post-field" shortcode, with item attribute:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-field

For example, you can get the value for the field "wpcf-link" from the child post type, using:


[wpv-post-field name='wpcf-link' item='@blueprint-text.child']

And the same shortcode can be used in the conditional statement, like this:


[wpv-conditional if="( '[wpv-post-field name='wpcf-link' item='@blueprint-text.child']' ne '' )"]
display link
[/wpv-conditional]

#2227149

Thank you! But it still displays nothing. I commented everything out in the content template for now and just added post title to be displayed. I expect that this should display the titles of the intermediary posts (which should be the same as the titles of the posts). But it doesn't display anything at all.

#2227439

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

If you'll check the "The Wartime Quartet" post, you'll see that the results from view are showing.
( screenshot: hidden link )

For the other two posts "Feminist philosophy" and "Postcolonial Theory, Race and Caste", there are no results showing, even though the relationship exists for those posts because none of them have any value in the custom field "Blueprint Order". As the view "display related posts" is set to order by this field "Blueprint Order", any intermediary posts, where this field is empty, will be ignored by this view.

> I expect that this should display the titles of the intermediary posts (which should be the same as the titles of the posts). But it doesn't display anything at all.

- The title of the intermediary posts is not the same as the parent or the child posts in the relationship, but it is generated automatically, using the IDs of the parent and the child posts.

I've added the following content in the view's loop, to make it more clear how to show information from the parent post ( Blueprints ) or the child post ( Posts ), using the item attribute:
( screenshot from the "The Wartime Quartet" post: hidden link )


<strong>Intermediary Post:</strong> [wpv-post-title] ( [wpv-post-id] )<br>
<strong>Parent Post:</strong> [wpv-post-title item="@blueprint-text.parent"] ( [wpv-post-id item="@blueprint-text.parent"] )<br>
<strong>Child Post:</strong> [wpv-post-title item="@blueprint-text.child"] ( [wpv-post-id item="@blueprint-text.child"] )<hr>

In this simple example, I'm just showing the title and IDs of the posts, depending on their roles, but you can show any field from those posts as needed.

#2229227
Capture.PNG

Thank you! This looks as it should on your screenshots, but I just don't see it on my end! I'm confused - have a look, this is the screenshot of the same thing on my end. Why could that be? I even cleaned cache and all (should that even matter?), but still can't see it.

Note - I went through the views and content templates to standardise the names, as some of the previous ones were created by Shane. Now I have:

Blueprints view - which displays a loop of all the blueprints
Loop item in Blueprints view - content template for those blueprints
Blueprint items view - which displays the specific texts within each blueprint
Loop item in Blueprint items view - content template for those texts

I made sure they are all linked as they should, I don't think I missed anything.

#2229531

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

In the content template "Single Blueprint", the old slug of the view was used in the view's shortcode:


[wpv-view name="display-related-posts"]

I've replaced it with the view's new slug and it is working, as expected now:


[wpv-view name="blueprint-items-view"]

1. "The Wartime Quartet"
hidden link

This is what I see on the front-end:
hidden link

And this is what I see on this post's edit screen, at the back-end:
hidden link

Please note that posts are linked and the values for the "Blueprint Order" fields are set, so these connections are showing in the view's results.

2. "Feminist philosophy"
hidden link

This is what I see on the front-end:
hidden link

And this is what I see on this post's edit screen, at the back-end:
hidden link

Please note that posts are linked but the values for the "Blueprint Order" fields are not set, so these connections are not showing in the view's results, and the "No items found" message is showing.

3. "Postcolonial Theory, Race and Caste"
hidden link

This is what I see on the front-end:
hidden link

And this is what I see on this post's edit screen, at the back-end:
hidden link

Again, please note that posts are linked but the values for the "Blueprint Order" fields are not set, so these connections are not showing in the view's results, and the "No items found" message is showing.

#2229647

Thanks! You're right, it does show it in the Single Blueprint now! The thing is, I wasn't even looking at this one before - this is a content template I want to use to display each blueprint separately, if a user follows a permalink to that blueprint.

But most of the time, I want the users to see the blueprints on the Blueprints page, where they do not use this content template. Instead, all blueprints are displayed as part of the Blueprints view, which uses the 'Loop item in Blueprints view' content template.

The Loop item in Blueprints view template should be pretty much identical to the Single Blueprint template - the only difference is that the latter doesn't have the accordion.

I now tried to call on the Single Blueprint content template in the Blueprints view, but the effect is the same:
- it displays the content correctly when it's on its own
- it just displays 'no content found' when it's part of the Blueprints view

So it seems like the problem is with nesting the views, no?

#2230541

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for making the requirement clear.

To make the view "Blueprint items view" work from inside another view as well as on the single blueprint page, you just need to change the query filter option to "The current post in the loop".
( screenshot: hidden link )

#2232125

It worked! Thank you for your help, this has been quite complex and you guided me through it very well, so I get what is going on now.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.