How do I create filters for custom post types?

Sorry the question might not make sense. I basically have a custom post type called “Projects” and I need to create a way to filter attributes of the project like “name”, “type”, etc.

For example if I have a project type called airplane, and another project type called airplane. I would want to be able to search for all projects with the name airplane, and display those in the loop.

I have looked into Taxonomies, but I do not know how to use them effectively.


Solutions Collecting From Web of "How do I create filters for custom post types?"

This is how I did it. If you have your custom post type as “projects”, and your category as “airplane”.

If you’re not planning on using the pre-build loop you would make another one called loop-projects.php. However, it’s not necessary.

                $args = array(
                    'numberposts'    => 5,
                    'post_type'      => 'projects',
                    'category_name'  => 'airplane');
                query_posts( $args );
                get_template_part( 'loop', 'projects' );

If I understand your description right, structure goes like this:

  • custom post type Project;
  • Type is taxonomy associated with Project CPT;
  • Airplane is term in Type taxonomy.

In current WP version you can query for this like (but will get changed in WP 3.1):

query_posts( array( 'post_type' => 'project','type' => 'airplane' ) );

See Taxonomy Parameters in Codex.

Simple as this:

// default filter:  
apply_filters( 'your_filter_name', $val_to_filter );  
// or, make it more specific:   
apply_filters( 'your_filter_name_'.$val_to_filter, $val_to_filter );

You can add this somewhere before your register_post_type call, so it would look like this:

// assuming, you got an $args array to make it easy to handle:  
$args['name'] = apply_filters('your_name_filter', $args['name'] );  
$args['name'] = apply_filters('your_name_filter_'.$args['name'], $args['name']);  
$args['type'] = apply_filters('your_type_filter', $args['type']);  
$args['type'] = apply_filters('your_type_filter_'.$args['type'], $args['type']);    
$name = $args['name'];    
register_post_type( $name, $args );