WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Searching by custom taxonomy (10 posts)

  1. gmorley
    Member
    Posted 1 year ago #

    I created a custom taxonomy, which works perfectly. Now I just need to figure out how to make it searchable. I followed the instructions on the Events Manager page http://wp-events-plugin.com/tutorials/creating-a-events-manager-add-on-a-complete-walkthrough/ by following step 6 and 7. It creates a dropdown but none of the artist names show up

    Here is the code I used

    // custom Artist taxonomy for events
    add_action( 'init', 'register_my_taxonomies', 0 );
    
    function register_my_taxonomies() {
    $labels = array(
        'name'                          => 'Artists',
        'singular_name'                 => 'Artist',
        'search_items'                  => 'Search Artists',
        'popular_items'                 => 'Popular Artists',
        'all_items'                     => 'All Artists',
        'parent_item'                   => 'Parent Artist',
        'edit_item'                     => 'Edit Artist',
        'update_item'                   => 'Update Artist',
        'add_new_item'                  => 'Add New Artist',
        'new_item_name'                 => 'New Artist',
        'separate_items_with_commas'    => 'Separate Artists with commas',
        'add_or_remove_items'           => 'Add or remove Artists',
        'choose_from_most_used'         => 'Choose from most used Artists'
        );
    
    $args = array(
        'label'                         => 'Artists',
        'labels'                        => $labels,
        'public'                        => true,
        'hierarchical'                  => true,
        'show_ui'                       => true,
        'show_in_nav_menus'             => true,
        'args'                          => array( 'orderby' => 'term_order' ),
        'rewrite'                       => array( 'slug' => 'events/artists', 'with_front' => false ),
        'query_var'                     => true
    );
    
    register_taxonomy( 'artists', EM_POST_TYPE_EVENT, $args );
    }
    
    // custom taxonomy search and display
    add_filter('em_events_get_default_search','artists_get_default_search',1,2);
    function artists_get_default_search($searches, $array){
    	if( !empty($array['artist']) && is_numeric($array['artist']) ){
    		$searches['artist'] = $array['artist'];
    	}
    	return $searches;
    }
    add_filter('em_events_get','artists_events_get',1,2);
    function artists_events_get($events, $args){
    	if( !empty($args['artist']) && is_numeric($args['artist']) ){
    		foreach($events as $event_key => $EM_Event){
    			if( !in_array($args['artist'],$EM_Event->artists) ){
    				unset($events[$event_key]);
    			}
    		}
    	}
    	return $events;
    }
    
    add_action('em_template_events_search_form_ddm', 'artists_search_form');
    function artists_search_form(){
    	$artists = (is_array(get_option('Artists'))) ? get_option('Artists'):array();
    	?>
    	<!-- START Styles Search -->
    	<select name="artist">
    		<option value=''>Artists</option>
    		<?php foreach($artists as $artist_id=>$artist_name): ?>
    		 <option value="<?php echo $artist_id; ?>" <?php echo ($_POST['artist'] == $artist_id) ? 'selected="selected"':''; ?>><?php echo $artist_name; ?></option>
    		<?php endforeach; ?>
    	</select>
    	<!-- END Styles Search -->
    	<?php
    }
    function artists_accepted_searches($searches){
    	$searches[] = 'artist';
    	return $searches;
    }
    add_filter('em_accepted_searches','artists_accepted_searches',1,1);

    http://wordpress.org/extend/plugins/events-manager/

  2. agelonwl
    Member
    Posted 1 year ago #

    you can download the complete tutorials of that at http://wp-events-plugin.com/downloads/2011/04/em-event-styles.txt

  3. gmorley
    Member
    Posted 1 year ago #

    It doesn't explain how to do it when you use a custom taxonomy. I will keep trying and post when I find the solution

  4. agelonwl
    Member
    Posted 1 year ago #

  5. gmorley
    Member
    Posted 1 year ago #

    I'm getting a lot closer. Here's how I made the drop down for the search:

    add_action('em_template_events_search_form_ddm', 'artists_search_form');
    function artists_search_form(){
    	$artists = (is_array(get_option('Artists'))) ? get_option('Artists'):array();
    	?>
    	<!-- START artists Search -->
    	<select name="artist" id="artist_search">
    		<option value="-1" selected="selected">Artists</option>
    		<?php
    		$taxonomies = array('artists');
    		$args = array('orderby'=>'count','hide_empty'=>true);
    		echo get_terms_dropdown($taxonomies, $args);
    		?>
    	</select>
    	<!-- END artists Search -->
    	<?php
    }
    
    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    
    	foreach($myterms as $term){
    		$root_url = get_bloginfo('url');
    		$term_taxonomy=$term->taxonomy;
    		$term_slug=$term->slug;
    		$term_name =$term->name;
    		$value = $term->term_id;
    		$output .="<option value='".$value."'>".$term_name."</option>";
    	}
    
    return $output;
    }

    Now I'm having an issue with this section of code:

    function my_em_artists_events_get($events, $args){
    	if( !empty($args['artist']) && is_numeric($args['artist']) ){
    		foreach($events as $event_key => $EM_Event){
    			if( !in_array($args['artist'],$EM_Event->artists) ){
    				unset($events[$event_key]);
    			}
    		}
    	}
    	return $events;
    }

    The problem is that $EM_Event->artists is not an array. Somehow I have to bring in the taxonomy. I'm going to try to use a similar technique as the function get_terms_dropdown

    Will keep you posted. Thanks for the links aglonwl, they've been pointing me in the right direction

  6. gmorley
    Member
    Posted 1 year ago #

    Alright here's the full code added to my functions.php file, fully working. One of my major sticking points was that I followed the other search drop-downs and set the "Artists" option in the select box to value="-1" (all the other ones did that, I figured it was correct). It caused me to run into a problem of not displaying any event ever.

    Copy the code and do a replace (ctrl-H in notepad++) on "artists" to "your-taxonomy-name" and replace "artist" with your "single-taxonomy-name"

    // custom Artist taxonomy for events
    add_action( 'init', 'register_my_taxonomies', 0 );
    
    function register_my_taxonomies() {
    $labels = array(
        'name'                          => 'artists',
        'singular_name'                 => 'artist',
        'search_items'                  => 'Search Artists',
        'popular_items'                 => 'Popular Artists',
        'all_items'                     => 'All Artists',
        'parent_item'                   => 'Parent Artist',
        'edit_item'                     => 'Edit Artist',
        'update_item'                   => 'Update Artist',
        'add_new_item'                  => 'Add New Artist',
        'new_item_name'                 => 'New Artist',
        'separate_items_with_commas'    => 'Separate Artists with commas',
        'add_or_remove_items'           => 'Add or remove Artists',
        'choose_from_most_used'         => 'Choose from most used Artists'
        );
    
    $args = array(
        'label'                         => 'Artists',
        'labels'                        => $labels,
        'public'                        => true,
        'hierarchical'                  => true,
        'show_ui'                       => true,
        'show_in_nav_menus'             => true,
        'args'                          => array( 'orderby' => 'term_order' ),
        'rewrite'                       => array( 'slug' => 'events/artists', 'with_front' => false ),
        'query_var'                     => true
    );
    
    register_taxonomy( 'artists', EM_POST_TYPE_EVENT, $args );
    }
    
    // custom taxonomy search and display
    add_action('em_template_events_search_form_ddm', 'artists_search_form');
    function artists_search_form(){
    	$artists = (is_array(get_option('artists'))) ? get_option('artists'):array();
    	?>
    	<!-- START artists Search -->
    	<select name="artist" id="artist_search">
    		<option value="" selected="selected">Artists</option>
    		<?php
    		$taxonomies = array('artists');
    		$args = array('orderby'=>'count','hide_empty'=>true);
    		echo get_terms_dropdown($taxonomies, $args);
    		?>
    	</select>
    	<!-- END artists Search -->
    	<?php
    }
    
    function my_em_artists_event_load($EM_Event){
    	global $wpdb;
    	$EM_Event->artists = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id='{$EM_Event->post_id}'", 0	);
    }
    add_action('em_event','my_em_artists_event_load',1,1);
    
    // And make the search attributes for the shortcode
    add_filter('em_events_get_default_search','my_em_artists_get_default_search',1,2);
    function my_em_artists_get_default_search($searches, $array){
    	if( !empty($array['artist']) ){
    		$searches['artist'] = $array['artist'];
    	}
    	return $searches;
    }
    
    add_filter('em_events_get','my_em_artists_events_get',1,2);
    function my_em_artists_events_get($events, $args){
    	if( !empty($args['artist'])  ){
    		foreach($events as $event_key => $EM_Event){
    			if( !in_array($args['artist'],$EM_Event->artists) ){
    				unset($events[$event_key]);
    			}
    		}
    	}
    	return $events;
    }
    
    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    
    	foreach($myterms as $term){
    		$root_url = get_bloginfo('url');
    		$term_taxonomy=$term->taxonomy;
    		$term_slug=$term->slug;
    		$term_name =$term->name;
    		$value = $term->term_id;
    		$output .="<option value='".$value."'>".$term_name."</option>";
    	}
    
    return $output;
    }
  7. pharmadave
    Member
    Posted 1 year ago #

    This is great. Helped me out a lot.
    I've added support for "reselecting the chosen item in the dropdown" by adding some code to set the "selected" attribute too....
    Modified get_terms_dropdown function below.
    Set
    '$search_values['cat'] = 'your chosen items slug'

    before calling the function.

    function get_terms_dropdown($taxonomies, $args)
    {
        global $search_values;
    
        $myterms = get_terms($taxonomies, $args);
        $output = "";
        foreach ($myterms as $term) {
            $root_url = get_bloginfo('url');
            $term_taxonomy = $term->taxonomy;
            $term_slug = $term->slug;
            $term_name = $term->name;
            $value = $term->term_id;
            if ($search_values['cat'] == $term->slug) {
                $selected = 'selected="selected" ';
            } else {
                $selected = '';
            }
            $output .= "<option value='" . $value . "' " . $selected . ">" . $term_name . "</option>";
        }
    
        return $output;
    }
  8. bentonwalker
    Member
    Posted 1 year ago #

    Hey gmorley or pharmadave,

    Is there any way to plug in the Artists taxonomy into the front end event editor? If I wanted to add an event for a specific artist how would I create the drop down selector to choose the artist from?

    Any help would be greatly appreciated.

    Thanks,

    Benton

  9. Burke Ingraffia
    Member
    Posted 1 year ago #

    How would you pass along the taxonomy value from a recurring event to each recurrence?

    I changed

    register_taxonomy( 'artists', EM_POST_TYPE_EVENT, $args );

    to

    register_taxonomy( 'artists', array(EM_POST_TYPE_EVENT, 'event-recurring'), $args );

    This is a start, but the values do not save in the recurrences.

    Any help you could provide would be appreciated.

  10. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    taxonomies in recurrences are tricky, because you need to save WP taxonomies to each post we created as a recurrence.

    I can't give a step-by-step here, but can point you to the relevant line:

    return apply_filters('em_event_save_events', false, $this, $event_ids, $post_ids);

    you need to hook into that filter, probably create an EM_Event object for each item in array $event_ids, replace the $EM_Event->categories with the $this->categories we pass on (which won't be called $this in your filter function), and then run $this->get_categories()->save();

    I haven't tested this, but in theory it should automatically do the hard work for you.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic