0

I have a custom post type and custom taxonomy set up using the code below in functions.php.

The URL writes correctly when clicking through on the archive pages on the front end and when clicking "View Post" in the admin edit screen. But when the post is returned in site search results, the custom taxonomy is literally missing. The URL on search results is http://www.example.com/foo//postname.

Any idea why the custom taxonomy in the URL would work in some situations but not in others? add_action( 'init', 'create_foo_posttype' ); function create_foo_posttype() {

    $tax_labels = array(
      'name' => _x( 'Foo Categories', 'taxonomy general name' ),
      'singular_name' => _x( 'Foo Category', 'taxonomy singular name' ),
      'all_items' => __( 'All Foo Categories' ),
      'parent_item' => null,
      'parent_item_colon' => null,
      'edit_item' => __( 'Edit Foo Category' ), 
      'update_item' => __( 'Update Foo Category' ),
      'add_new_item' => __( 'Add New Foo Category' ),
      'new_item_name' => __( 'New Foo Category Name' ),
    ); 


    register_taxonomy('foo_categories','foo',array(
      'labels' => $tax_labels,
      'hierarchical' => true,
      'has_archive' => true,
      'show_admin_column' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'foo' )   
    ));


    register_post_type( 'foo',
        array(
            'labels' => array(
                'name' => __( 'Foo' ),
                'singular_name' => __( 'Foo' )
            ),
            'public' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 100,
            'capability_type' => 'post',
            'supports' => array( 'title', 'author', 'thumbnail', 'trackbacks', 'revisions' ),
            'taxonomies' => array( 'foo_categories' ),
            'has_archive' => true,
            'rewrite' => array( 'slug' => 'foo/%foo_categories%')     
        )
    );
  flush_rewrite_rules();
}


add_action( 'init', 'cust_rewrite_init' );
function cust_rewrite_init() {
    $GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}

add_filter( 'page_rewrite_rules', 'cust_rewrite_collect_page_rewrite_rules' );
function cust_rewrite_collect_page_rewrite_rules( $page_rewrite_rules )
{
    $GLOBALS['cust_rewrite_page_rewrite_rules'] = $page_rewrite_rules;
    return array();
}

add_filter( 'rewrite_rules_array', 'cust_rewrite_prepend_page_rewrite_rules' );
function cust_rewrite_prepend_page_rewrite_rules( $rewrite_rules )
{
    return $GLOBALS['cust_rewrite_page_rewrite_rules'] + $rewrite_rules;
}
jlibs
  • 13
  • 7
  • Did you flush the permalinks cache by going to Settings > Permalinks and then clicking save at the bottom without making any changes? – kel Jan 20 '15 at 15:09
  • Yes, I did. Unfortunately that didn't make a difference, but thanks for the suggestion. – jlibs Jan 20 '15 at 15:41

1 Answers1

0

I found the problem. It wasn't in the code I posted originally. It was here:

        add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
    function cust_permalink_structure($post_link, $post, $leavename, $sample)
    {
        if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
            $post_type_term = get_the_terms( $post->ID, 'foo_categories' );
            $post_link = str_replace( '%foo_categories%', $post_type_term[0]->slug, $post_link ); 
        }
        return $post_link;
    }

Using $post_type_term[0]->slug; worked if a post had more than one tax value, but failed when the post had only one (which all of my posts do). To fix it, I used array_pop instead of the other array call and it works fine. The resolved code:

        add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
    function cust_permalink_structure($post_link, $post, $leavename, $sample)
    {
        if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
            $post_type_term = get_the_terms( $post->ID, 'foo_categories' );
            $one_term = array_pop($post_type_term);
            $post_link = str_replace( '%foo_categories%', $one_term->slug, $post_link ); 
        }
        return $post_link;
    }
jlibs
  • 13
  • 7