1

I created a custom post_type called Events, where I can show the Events(URL:HTTP://localhost.com/events) that are load by the archive-events.php that I created.

Now I have created a custom taxonomy called counties that I want to use to filter Events by the county in which they are located. I'm able to apply counties to specific Events, but I have no idea how to show them. I have tried to create a taxonomy-counties.php

But I have no idea how to test it or debug it since if I try to go to HTTP://localhost.com/events/county or HTTP://localhost.com/county it takes me to a '404 Page Not Found'.

This is the code that I used to create the taxonomy:

add_action('init', 'create_counties_hierarchical_taxonomy', 0);

function create_counties_hierarchical_taxonomy() {
    $labels = array(
    'name' => _x( 'Counties', 'taxonomy general name' ),
    'singular_name' => _x( 'County', 'taxonomy singular name' ),
    'search_items' => __( 'Search Counties' ),
    'all_items' => __( 'All Counties' ),
    'parent_item' => __( 'Parent County' ),
    'parent_item_colon' => __( 'Parent County:' ),
    'edit_item' => __( 'Edit County' ), 
    'update_item' => __( 'Update County' ),
    'add_new_item' => __( 'Add New County' ),
    'new_item_name' => __( 'New County Name' ),
    'menu_name' => __( 'Counties' ),
    );
    
    // Register Taxonomy
    register_taxonomy('counties',array('events'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'county' ),
  ));
}

I have tried going through the WordPress developer documentation but unfortunately haven't been able to understand this and found an answer.

Thanks!

3
  • What you want to show? Do you want to show list of counties and after that it will list of events associate with that? Aug 25, 2020 at 18:34
  • Yeah, my idea was for the user to be able to select a county and to display all the Events in that county
    – Tincho N
    Aug 25, 2020 at 18:46
  • To display counties in a template you need to use get_terms() of WP and by iterating the each county/terms you can get specific county URL by calling get_term_link() function of WP. Aug 25, 2020 at 19:25

1 Answer 1

0

Ok, i did work for you:

First, I registered custom post type and taxonomy:

add_action('init', 'my_init');
add_action('admin_init', 'my_admin_init');

function my_admin_init() {
    $role = get_role( 'administrator' );
  $role->add_cap( 'edit_event' );
  $role->add_cap( 'read_event' );
  $role->add_cap( 'delete_event' );
  $role->add_cap( 'edit_events' );
  $role->add_cap( 'read_events' );
  $role->add_cap( 'delete_events' );
  $role->add_cap( 'edit_others_events' );
  $role->add_cap( 'publish_events' );
  $role->add_cap( 'read_private_events' );
  $role->add_cap( 'delete_private_events' );
  $role->add_cap( 'delete_published_events' );
  $role->add_cap( 'edit_private_events' );
  $role->add_cap( 'edit_published_events' );
  $role->add_cap( 'create_events' );
  $role->add_cap( 'delete_others_events' );
  $role->add_cap( 'delete_others_event' );
}
function my_init() {
    global $post;
    $labels = array(
        'name'                => __( 'event', 'webto' ),
        'singular_name'       => __( 'Header component', 'webto' ),
        'menu_name'           => __( 'Event', 'webto' ),
        'parent_item_colon'   => __( 'Parental:', 'webto' ),
        'all_items'           => __( 'event', 'webto' ),
        'view_item'           => __( 'View', 'webto' ),
        'add_new_item'        => __( 'Add new event', 'webto' ),
        'add_new'             => __( 'Add new', 'webto' ),
        'edit_item'           => __( 'Edit event', 'webto' ),
        'update_item'         => __( 'Update event ', 'webto' ),
        'search_items'        => __( 'Find event', 'webto' ),
        'not_found'           => __( 'Not found', 'webto' ),
        'not_found_in_trash'  => __( 'Not found in the bin', 'webto' ),
    );
    $args = array(
        'labels'              => $labels,
        'supports'            => array('title'),
        'public'              => false,
        'publicly_queryable' => false,
        'capability_type' => array('event', 'events'),
        'map_meta_cap' => true,
        'show_in_menu' => true,
        'rewrite' => array( 'slug' => 'events' ),
        'query_var' => true,
        'show_in_rest' => false,
        'show_ui' => true,
        'hierarchical' => false,
        'menu_position'       => 1,
        'menu_icon'           => 'dashicons-welcome-widgets-menus',
    );
    register_post_type( 'events', $args );

    $labels = array(
        'name' => _x( 'county', 'taxonomy general name' ),
        'singular_name' => _x( 'county', 'taxonomy singular name' ),
        'search_items' => __( 'Search county' ),
        'all_items' => __( 'All counties' ),
        'parent_item' => __( 'Parent county' ),
        'parent_item_colon' => __( 'Parent county:' ),
        'edit_item' => __( 'Edit county' ),
        'update_item' => __( 'Update county' ),
        'add_new_item' => __( 'Add New county' ),
        'new_item_name' => __( 'New county Name' ),
        'menu_name' => __( 'counties' )
    );

    // Register Taxonomy
    register_taxonomy('county', array('events'), array(
        'hierarchical' => true,
        'public'        => true,
        'publicly_queryable'        => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true
    ));
    flush_rewrite_rules();
}

I got the following:

enter image description here

Create your posts and assign counties to them

enter image description here

The countieson the counties page will have links to view

We can create an archive page for specific counties

taxonomy-county.php

Also display yours posts

// templates/your-template/taxonomy-county.php
<?php
global $post;
$county = get_queried_object();


$args = array(
  'post_type' => 'events',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => $county->taxonomy,
      'field' => 'term_id',
      'terms' => $county->term_id
    )
  )
);
// Get posts by current county
$events = get_posts($args);

// All counties
$all_counties = get_terms($county->taxonomy);

foreach($all_counties as $county) {
    $args = array(
      'post_type' => 'events',
      'numberposts' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => $county->taxonomy,
          'field' => 'term_id',
          'terms' => $county->term_id
        )
      )
    );
    // get posts by county
    $events = get_posts($args);
}
1
  • Thanks for the help! I was able to found the solution to my problem. I don't know exactly why but I had to reset the permalinks and it worked.
    – Tincho N
    Aug 29, 2020 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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