• [ Moved to the Fixing WordPress sub-forum. ]

    Hi,

    We are using the below code to use the previous and next post links by menu order only but the issue we are having is that we have menu orders set for each category so need the code to only go to the next or previous custom post (WooCommerce product) within the same category as currently the order is not right.

    In addition we have installed Yoast SEO as this now gives us the feature to set a primary category which we have done for all items in this category https://www.nebulasdev.co.uk/amazon/title-category/appearances/comic-cavalcade but this still isn’t working so I believe if we can ensure it only uses items from the same category this should work.

    (Code taken from this post https://1fix.io/blog/2014/09/09/get-right-previous_post_link-when-order-posts-by-menu_order/).

    function my_previous_post_where() {
    	global $post, $wpdb;
    	return $wpdb->prepare( "WHERE p.menu_order < %s AND p.post_type = %s AND p.post_status = 'publish'", $post->menu_order, $post->post_type);
    }
    add_filter( 'get_previous_post_where', 'my_previous_post_where' );
    function my_next_post_where() {
    	global $post, $wpdb;
    	return $wpdb->prepare( "WHERE p.menu_order > %s AND p.post_type = %s AND p.post_status = 'publish'", $post->menu_order, $post->post_type);
    }
    add_filter( 'get_next_post_where', 'my_next_post_where' );
    function my_previous_post_sort() {
    	return "ORDER BY p.menu_order desc LIMIT 1";
    }
    add_filter( 'get_previous_post_sort', 'my_previous_post_sort' );
    function my_next_post_sort() {
    	return "ORDER BY p.menu_order asc LIMIT 1";
    }
    add_filter( 'get_next_post_sort', 'my_next_post_sort' );

    We are unable to use post date as we are using this for timelines and setting this to when the issue was published.

    Any help would be much appreciated.

    Thanks,

    James Isles

    [ No bumping please. ]

  • The topic ‘Next/Prev Custom Post Links By Menu Order and in Same Category Only’ is closed to new replies.