Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari

    (@msaari)

    “Category” is actually just a type of taxonomy, so the answer is yes.

    esc_attr(add_query_arg(array('taxonomy' => 'taxonomy slug', 'terms' => 'taxonomy term slug', 's' => get_search_query()), $url));

    Thread Starter Mark

    (@markob17)

    Hi Mikko,

    Thank you for replying to my question so quickly, appreciate it. Unfortunately, I’m brand new to PHP and can’t quite get this to work.

    Do I have to specify all the values you added to the array (taxonomy slug, terms, and taxonomy term slug)? Or will one of the values be sufficient, for example just the taxonomy slug?

    I know what a taxonomy slug is but I’m not quite sure what the ‘terms’ or ‘taxonomy term slug’ is.

    Would you be able to provide an example (for a newbie!) based off the code I used to register my taxonomy? If so, it is below. Thanks!

    function add_trainers_taxonomies() {
    	// Add new "trainers" taxonomy to Pages
    	register_taxonomy('trainers', 'page', array(
    		// Hierarchical taxonomy (like categories)
    		'hierarchical' => true,
    		// This array of options controls the labels displayed in the WordPress Admin UI
    		'labels' => array(
    			'name' => _x( 'Personal Trainers', 'taxonomy general name' ),
    			'singular_name' => _x( 'Location', 'taxonomy singular name' ),
    			'search_items' =>  __( 'Search Locations' ),
    			'all_items' => __( 'All Locations' ),
    			'parent_item' => __( 'Parent Location' ),
    			'parent_item_colon' => __( 'Parent Location:' ),
    			'edit_item' => __( 'Edit Location' ),
    			'update_item' => __( 'Update Location' ),
    			'add_new_item' => __( 'Add New Location' ),
    			'new_item_name' => __( 'New Location Name' ),
    			'menu_name' => __( 'Personal Trainers' ),
    		),
    		// Control the slugs used for this taxonomy
    		'rewrite' => array(
    			'slug' => 'personal-trainers', // This controls the base slug that will display before each term
    			'with_front' => false, // Don't display the category base before "/locations/"
    			'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    		),
    	));
    }
    add_action( 'init', 'add_trainers_taxonomies', 0 );

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Plugin Author Mikko Saari

    (@msaari)

    In this case, taxonomy slug would be “trainers”. “Taxonomy term slug” would be the slug of the taxonomy term you want to use to filter the search (ie. it corresponds to category ID number). So, if you have a personal trainer called “John Doe”, the taxonomy filter might look like this:

    esc_attr(add_query_arg(array('taxonomy' => 'trainers', 'terms' => 'john-doe', 's' => get_search_query()), $url));

    Thread Starter Mark

    (@markob17)

    Thanks for the explanation Mikko! For some reason I still can’t get it to work using the example you just provided, however, I messed around and ended up trying the values below and I was able to get it to work. Not sure why I couldn’t get it to work using your example. Either way, it is working for me now. Thanks again for your help!

    $url = esc_attr(add_query_arg(array(‘I PUT TAXONOMY NAME HERE’ => ‘I PUT TAXONOMY SLUG HERE’, ‘s’ => get_search_query()), $url));

    Plugin Author Mikko Saari

    (@msaari)

    That works, but WordPress documentation claims that’s deprecated since version 3.1. Looks like it still works, though.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Can search results be filtered by taxonomy?’ is closed to new replies.