Why would WordPress use ‘attachment’ in a custom post type’s query string?
Albeit I have a slightly contorted structure in place, I can’t seem to find where ‘attachment’ comes into play and how to change that behavior.
My custom post type ‘fb_entry’ has the custom taxonomy ‘issue’. Posts from ‘fb_entry’ are rewritten with the slug ‘journal/%issue%’ where %issue% is the term name. I also have a second custom post type called ‘fb_issue’ whose posts are rewritten with the slug ‘journal’. There is also a page with the slug ‘journal’.
Here’s how this is intended to work:
- User visits /journal
- User browses list of posts from fb_issue and makes selection
- User is taken to /journal/fb_issue
- Here, user browses list of posts from fb_entry with ‘issue’ terms that match the fb_issue slug
- User makes selection and views chosen post from fb_entry
- Ideally, user has URL /journal/issue-term-name/fb_entry-post-name
This all seems to work fine until the final step whereat the user reaches a 404 page. Debug Toolbar tells me that the request is:
- /journal/issue-term-name/fb_entry-post-name
But the query string is:
- attachment=fb_entry-post-name
However, none of my queries call for attachments.
EDIT
Here’s the post type registration. Standard, I think:
Standardly, I think:
/* Create the 'Entry' post type */
function create_fb_entry(){
register_post_type( 'fb_entry',
array(
'labels' => array(
'name' => 'Entries',
'singular_name' => 'Entry',
'add_new' => 'Add New Entry',
'add_new_item' => 'Add New Entry',
'edit_item' => 'Edit Entry',
'new_item' => 'New Entry'
),
'public' => true,
'rewrite' => array(
'slug' => 'journal/%issue%',
'with_front' => false,
),
'supports' => array(
'title',
'editor',
'excerpt',
'page-attributes',
'revisions',
'thumbnail'
)
)
);
}
add_action('init', 'create_fb_entry');
function create_fb_entry_issue() { /* Create the 'Issue' taxonomy */
$labels = array(
'name' => _x( 'Issue', 'issue' ),
'singular_name' => _x( 'Issue', 'issue' ),
'search_items' => __( 'Search Issues' ),
'all_items' => __( 'All Issues' ),
'parent_item' => __( 'Parent Issue' ),
'parent_item_colon' => __( 'Parent Issue:' ),
'edit_item' => __( 'Edit Issue' ),
'update_item' => __( 'Update Issue' ),
'add_new_item' => __( 'Add New Issue' ),
'new_item_name' => __( 'New Issue' )
);
register_taxonomy( 'issue',
array( 'fb_entry' ),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'issue',
'with_front' => false
)
)
);
}
add_action( 'init', 'create_fb_entry_issue', 0 );
EDIT 2
For further clarity, here’s the code I’m using to rewrite %issue%:
function fb_issue_permalink($permalink, $post_id, $leavename) {// Fix dynamic 'issue'/'fb_entry' slug in 'fb_entry' permalink
if (strpos($permalink, '%issue%') === FALSE) return $permalink;
// Get post
$post = get_post( $post_id );
if( ! $post ) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms( $post->ID, 'issue' );
if( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) ) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'unpublished'; // Substitute 'journal issue' in slug if 'journal entry' is not categorized
return str_replace('%issue%', $taxonomy_slug, $permalink);
}
add_filter('post_link', 'fb_issue_permalink', 10, 3);
add_filter('post_type_link','fb_issue_permalink', 10, 3);
I was able to fix this and designate the order of rewrite rules using this class: http://hmn.md/says/2012/11/08/wordpress-rewrite-rules-hm-core-style/