Skip Navigation

[Resolved] link to prev and next post based in a ordering custom field

This support ticket is created 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 2 voices.

Last updated by Christian Cox 7 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#539736

Bsd
this is a continuation of
https://toolset.com/forums/topic/how-to-order-a-view-by-a-specific-field-orde/
Now i need to continue in a new chalenge:
based on what we did (ordering each category by a custom field), i would like to add to my post template a link to the next post and previous post.
But this "next" and previous has to be according to our custom order field.
For each category (10 we have) we have a specific order custom field...
also some posts pertence to 2 categories.
Is it possible?
Thanks again!

#539887

Hi, I will need to do a bit of investigation to find the best solution here. How should I handle the case where a post belongs to more than one category? Which category sort order is reflected in the next / previous buttons on such a post?

#539898

it should take the category choosed from previus page he came from in case he came from a archive category page
in case that he cames directly to the post - it should give him the next prev according to a new custom field that fixes the "default order category custom filed"

#540221

it should take the category choosed from previus page he came from
Will URL parameters be acceptable? For example, adding ?sortby=field1, or ?sortby=field2 when user clicks the Next and Previous links?

#540241

should be - but if i press ina category link - how should this parameter should be passed automatically?

#540315

how should this parameter should be passed automatically?
I would prefer to keep this ticket focused on the next and previous buttons. Let us address the category term links in another ticket, since it's a different problem.

I'm not going to be able to finish this today, and I'm unavailable on Fridays and Saturdays. I will return Sunday with an update for you. Thanks for your patience!

#541132

Please add the following code to functions.php:

add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
function wpv_post_param_shortcode( $atts ) {
  if ( !empty( $atts['var'] ) && isset($_GET[$atts['var']]) ) {
    $var = (array)$_GET[$atts['var']];
    return esc_html( implode( ', ', $var ) );
  }
}

add_shortcode( 'toolset_get_custom_pagination_link', 'toolset_get_custom_pagination_link_func');
function toolset_get_custom_pagination_link_func($atts)
{
  global $post;
  $default = 'wpcf-order-c';
  $order = $atts['order'];
  $view_ID = 9;
  $orderby = $atts['orderby'];
  $link = '';
  $label = $order=='asc' ? 'Next' : 'Previous';
  $args = array('order' => $order, 'orderby_as' => 'numeric');
  $args['orderby'] = $orderby ? 'field-' . $orderby : 'field-' . $default;
  $posts = get_view_query_results( $view_ID, null, null, $args );
  if(isset($posts[0])) {
    $i=0;
    foreach($posts as $thisPost) {
      if ( $post->ID == $thisPost->ID) {
        $offset = $i;
        break;
      }
      $i++;
    }
    $args['offset'] = $offset + 1;
    $args['limit'] = 1;
    $sortedPosts = get_view_query_results( $view_ID, NULL, NULL, $args );
    if(isset($sortedPosts[0])) {
      $sortedPost_id = isset($sortedPosts[0]) ? $sortedPosts[0]->ID : -1;
      $link = '<a href="' . get_permalink($sortedPost_id);
      $link .= $orderby ? '?sortby=' . $orderby : '';
      $link .= '">' . $label . '</a>';
      $link .= ': <span style="font-size: .8em;">' . get_permalink($sortedPost_id);
      $link .= $orderby ? '?sortby=' . $orderby : '';
      $link .= '</span>';
    }
  }
  return $link;
}

The first section is a shortcode that will allow us to access URL parameters. The second is another custom shortcode that will help you display your next and previous links using a View. Modify the following values:
'wpcf-order-c' => The slug of your default sort order custom field, appended to 'wpcf-',
9 => The ID of your new View (see below),
$link => You're welcome to modify the format of this link however you would like once it's working appropriately. I'd recommend starting like this.

Create a new View which will be used in your next and previous links to get the correct post URLs. This View should show only the specific post type you want to include, and it should not have any filters. The Loop Output editor should include only the required content. Nothing will be output for this View anyway, we will just use it to get results. Here's a sample Loop:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop></wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Finally, place the following code in your Content Template for these posts:

[toolset_get_custom_pagination_link order="desc" orderby="[wpv-post-param var='sortby']"]<br />
[toolset_get_custom_pagination_link order="asc" orderby="[wpv-post-param var='sortby']"]

Now you will see a "Previous" link and a "Next" link for your posts, pointing to the appropriate post in sequence.

Finally, test this out by navigating to some posts directly. You should not see '?sortby=' in the URL query string. These posts should be sorted by your custom default sort order field. Next, try adding some sorting parameters to the URL: ?sortby=wpcf-mycustomsortfield

You will notice that once you have applied this sort order, it will be applied to any following post URLs. This way sort order will be respected as long as someone navigates with the next / previous buttons. Let's give this a shot and see how it's working for you, then we can make tweaks as needed.

#541541

bsd

i tried it , but it doesn't work
i see the links to next and previous - but i have no idea according to what they appear.
i tried adding the parameter in url - and also dopesn't work
it brings next and prev links from other categories...
to you want to get in again in the site?

#541648

Yes, please provide login credentials in the private reply fields here.

#542096

Thanks, I will try to set up a clone of your site so I can test the code on my own local environment and get it working correctly.

Can you share:
- URL to a post where I can see Next and Previous links on the front-end of your site
- URL to the BB post template in wp-admin

#542100

- URL to a post where I can see Next and Previous links on the front-end of your site:

hidden link

- URL to the BB post template in wp-admin
hidden link
or
hidden link

thanks!

#542520
Screen Shot 2017-06-28 at 9.44.23 AM.png

Sorry for the delay responding, it takes a long time for me to troubleshoot because of the language difference. Please change your View Query Options here:

/wp-admin/admin.php?page=views-editor&view_id=8233

Uncheck the setting "Don't include current page in query result". This seems to be the source of the problem. I tested on my clone of your site after making this change. First I started with default sorting:
Sort by default wpcf-post-place-faith-in-the-era-of-knowledge
Start here: wp-admin/post.php?post=8268&action=edit - value = 9999999
Click Previous: wp-admin/post.php?post=7825&action=edit - value = 9999998
Click Previous again: wp-admin/post.php?post=4776&action=edit - value = 66
Click Previous again: wp-admin/post.php?post=4940&action=edit - value = 65
Click Next: wp-admin/post.php?post=4776&action=edit - value = 66
Click Next: wp-admin/post.php?post=7825&action=edit - value = 9999998
Click Next: wp-admin/post.php?post=3238&action=edit - value = 9999999
This seems out of order, but the real issue is you have multiple posts with the same value in the custom field. This will break the sorting algorithm.

Please try some tests on your own and let me know the results after making these changes. Thank you.