5

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:

  1. User visits /journal
  2. User browses list of posts from fb_issue and makes selection
  3. User is taken to /journal/fb_issue
  4. Here, user browses list of posts from fb_entry with 'issue' terms that match the fb_issue slug
  5. User makes selection and views chosen post from fb_entry
  6. 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);
  • How is your CPT registered? – s_ha_dum Sep 2 '13 at 21:46
  • Posted in the edit.. Both the CPT and tax. – bode well carapace Sep 2 '13 at 23:03
  • attachment is set because it thinks fb_entry-post-name is an attachment, attached to the post issue-term-name, so your rewrite rules aren't quite right for some reason. – Milo Sep 3 '13 at 0:34
  • That makes sense. Is there something with rewrite rules that would look to attachment by default?I think just about all of the code being used, aside from the registration of fb_issue is listed above. – bode well carapace Sep 3 '13 at 0:45
0

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/

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.