Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I made a custom post type and a custom taxonomy, but I have a problem.

When I access the url http://www.ithemes.co.kr/?shopcat=galaxy-s, it works well. But when I access the url http://www.ithemes.co.kr/?shopcat=5, it returns a 404 page. galaxy-s is the taxonomy term slug, but I want to access it by term_id, like you can with a category: cat=1. I prefer cat=1.

What can I do?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

This is not supported by default, but you can convert the numeric terms back to slugs with the pre_get_posts hook. I tested this with WP 3.0.1, but in 3.1 the taxonomy query handling changed, so I don't know whether this will work by default or if there is a better way to do it.

add_action( 'pre_get_posts', 'wpse6066_pre_get_posts' );
function wpse6066_pre_get_posts( &$wp_query )
{
    if ( $wp_query->is_tax ) {
        if ( is_numeric( $wp_query->get( 'term' ) ) ) {
            // Convert numberic terms to term slugs
            $term = get_term_by( 'term_id', $wp_query->get( 'term' ), $wp_query->get( 'taxonomy' ) );
            if ( $term ) {
                $wp_query->set( 'term', $term->slug );
            }
        }
    }
}

Bizarre that you prefer the numeric version, many would choose the term slug for SEO reasons.

share|improve this answer
    
Wow, it's too work!!!! you save my time:) so thank you^^ –  Ssong Won Jan 3 '11 at 8:50
    
@Ssong Won: You can accept this answer, this way others can see it is solved for you. It also gives your and my "reputation score" a boost. I'm happy it works for you, and I hope to see other good questions and answers from you on this site! –  Jan Fabry Jan 3 '11 at 11:00

These few lines are very useful for wp_dropdown_categories used in custom taxonomies for backend (edit.php) list subselection. Because values in list are term id and not usable as is for sub selection in query... below parts of code used in a class managing custom post type and custom taxonomy... many thanks for the future new version 2.0 of xili-dictionary plugin...

add_action( 'restrict_manage_posts', array(&$this,'restrict_manage_writer_posts') );
add_action( 'pre_get_posts', array(&$this,'wpse6066_pre_get_posts' ) );

function restrict_manage_writer_posts () {

        $selected = "";
        if ( isset ( $_GET['writer_name'] )  ) { 
            $selected = $_GET['writer_name'];
        }
        $dropdown_options = array(
                    'taxonomy' => 'writer',
                    'show_option_all' => __( 'View all writers' ),
                    'hide_empty' => 0,
                    'hierarchical' => 1,
                    'show_count' => 0,
                    'orderby' => 'name',
                    'name' => 'writer_name',
                    'selected' => $selected
                );
        wp_dropdown_categories( $dropdown_options );

    }
    /** 
     * to fixes wp_dropdown_categories id value in option
     * thanks to http://wordpress.stackexchange.com/questions/6066/query-custom-taxonomy-by-term-id 
     */
    function wpse6066_pre_get_posts( &$wp_query )
    {
        if ( $wp_query->is_tax ) {  ;
            if ( is_numeric( $wp_query->get( 'writer_name' ) ) ) {
                // Convert numberic terms to term slugs for dropdown

                $term = get_term_by( 'term_id', $wp_query->get( 'writer_name' ), 'writer' );

                if ( $term ) {
                    $wp_query->set( 'writer_name', $term->slug );
                }
            }
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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