WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Next / Previous Post Link PHP Error (2 posts)

  1. Adam
    Member
    Posted 1 year ago #

    I'm trying to get the next_post_link to work with a custom post type single.php page.

    My code is:

    <?php next_post_link('%link'); ?>
    <?php previous_post_link('%link'); ?>

    For some reason, I keep getting this error message along with my links (which, strangely enough, do link properly).

    Warning: call_user_func_array(): First argument is expected to be a valid callback, 'get_custom_adjacent_post_join' was given in /wp-includes/plugin.php on line 173

    This is different from the next_posts_link (posts with an "s"). Any thoughts?

  2. Adam
    Member
    Posted 1 year ago #

    I guess I'll answer my own question again. For anyone's future reference on how to get next_post_link to work with custom post types, just stick this in the theme's functions.php file:

    <?php
    	function get_custom_adjacent_post_join($join, $in_same_cat) {
    		global $post, $wpdb;
    
    		// we don't process empty post and default post types
    		if (empty($post) || in_array($post->post_type, array('post', 'page', 'attachment', 'revision', 'nav_menu_item')))
    			return $join;
    
    		// here we specify custom post types and their corresponding taxonomies
    		$taxonomies = array(
    			'your_custom_post_type'	=> 'your_taxonomy'
    		);
    
    		$current_taxonomy = $taxonomies[$post->post_type];
    
    		if ($in_same_cat) {
    		$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
    
    		if ( $in_same_cat ) {
    			$cat_array = wp_get_object_terms($post->ID, $current_taxonomy, array('fields' => 'ids'));
    			$join .= " AND tt.taxonomy = '$current_taxonomy' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
    		}
    		}
    		return $join;
    	}
    	add_filter('get_previous_post_join', 'get_custom_adjacent_post_join', 10, 2);
    	add_filter('get_next_post_join', 'get_custom_adjacent_post_join', 10, 2);
    	?>

Topic Closed

This topic has been closed to new replies.

About this Topic