Skip Navigation

[Resolved] Using a custom field for natural ordering custom post

This support ticket is created 6 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)

Author
Posts
#630305

Tell us what you are trying to do?
I have some custom types with hierarchical relationship :
trip > step > POI

When i display a step, by default it uses the editing date of the post (the step) to define the natural order of the post. So when i display the post like this :
hidden link

The previous and next link point to irrelevant ones. I want that the custom field 'Date' will be used to order the posts of this type. So the previous link will point to the previous step (earlier date than the date of the current one but with the same parent (trip)) and the next link will point to the next one (later date).

Is there any documentation that you are following?
I tried this solution : https://toolset.com/forums/topic/previous-next-within-parentchild-relationship/
i can't figure out what orderby parameter i need use. But maybe i can force my custom 'Date' field to be used in place of the standard date.

For the theme i used a specific one but i can switch to twentyseventeen, it gives same result.

What is the link to your site?

hidden link

#630458

Hello,

It needs custom codes, you will need to add orderby parameter into the wordpress query, for example, modify this line in your php codes from:

$children = get_posts(array('post_type' => $childslug, 'meta_key' => '_wpcf_belongs_' . $parentslug . '_id', 'meta_value' => $parent, 'numberposts' => -1));

To:

$args = array(
	'post_type'  => $childslug,
	'meta_key'   => 'wpcf-date',
	'orderby'    => 'meta_value_num',
	'order'      => 'ASC',
	'meta_query' => array(
		array(
			'key'     => '_wpcf_belongs_' . $parentslug . '_id',
			'value'   => $parent,
		),
	),
	'numberposts' => -1
);
$children = get_posts($args);

Please replace "date" with the custom "Date" field slug, and test again.

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters