WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Navigation between custom post types

[Resolved] Navigation between custom post types

  • Hello,

    I need to navigate between custom post types, only into a custom taxonomy.

    When I try to use…

    previous_post_link( ‘%link’, ‘<span class=”meta-nav”>’ . _x( ‘ ‹ ‘, ‘Previous post link’, ‘twentyten’ ) . ‘</span> %title’, TRUE );

    The line doesn’t appear. This line of code is taken from TwentyTen and I have added the TRUE parameter in order to only navigate between the same custom taxonomy.

    Any idea of how can this be done?

    Thank you very much!

Viewing 10 replies - 1 through 10 (of 10 total)
  • I have to second this, I can’t make this to work either. When I turn on the debug, there’s an SQL error reported by WordPress made by those two functions. The feed does not list items having custom post type either…

    I think I have found the solution, it looks like WordPress developers didn’t add a custom post type check to get_adjacent_post. We might get around using filter fortunately. If anyone’s interested I will post the filter.

    Thank you very much for your answer.

    Will be nice to know the solution, as I suppose that navigate between custom post types will be useful when using WordPress as CMS.

    iwanttobelieve, your help is really appreciated in advance. Thank you!

    First of all, in order to make this work, you must hack into the core file wp-includes/link-template.php. Find $posts_in_ex_cats_sql = “AND tt.taxonomy = ‘category'”; and change it to: $posts_in_ex_cats_sql = ”; It’s not that we can’t create a filter, it’s just too troublesome to do so, trust me. Also, this filter is designed for ppl who don’t have excluded_categories, if you have, tell me and I will provide another filter.

    Okay, we need 1 filter, assuming that you have already been familiar with creating plugin, I will just post the filter, ok?

    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;
    		}

    In somewhere of your plugin, type:
    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);

    Please note this line:
    $taxonomies = array(‘your_custom_post_type’ => ‘your_taxonomy’);

    If you would like a movie post type and a Director as taxonomy, you would change it to:
    $taxonomies = array(‘movie’ => ‘director’);
    In case you need to add more, simply add another item for that array.

    Hope that will help!

    iwanttobelieve thank you very much for your assistance.

    Is possible to define this function in functions.php file? I’m not really used to create plugins for WP and the process confuses me a little.

    Also, will be fine to insert this code:

    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);

    in the template?

    Thank you!

    It actually depends on your installation, however that should be fine in most cases. You can either put them both in functions.php or filter in functions.php and add_filter in template, it will work :).

    I hope to get this work one day 😀

    Thank you very much for your assistance.

    I have been able to solve this using the Ambrosite Next/Previous Post Link Plus.

    Thank you for your assistance.

    @iwanttobelieve:
    I have tried using your filter and am getting the following error…

    implode() [function.implode]: Invalid arguments passed in /home/blah/blah/blah/myurl/functions.php

    its this line that seems to be the issue:
    $join .= " AND tt.taxonomy = '$current_taxonomy' AND tt.term_id IN (" . implode(',', $cat_array) . ")";

    I noticed that @tradiart solved his initial problem using the Ambrosite Next/Previous Post Link Plus plug-in, but that is also cause me problems. So could you please advise me as to what I might need to change in order to get this to work. thank you in advance!

    I have updated my plugin to work with custom taxonomies. Details on the fix are posted in this thread:

    http://wordpress.org/support/topic/plugin-ambrosite-nextprevious-post-link-plus-seeking-beta-testers

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Navigation between custom post types’ is closed to new replies.
Skip to toolbar