Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tremendous Taxonomies

Tremendous Taxonomies

Taxonomies have been around in WordPress for a long time as a way of grouping things together, but did you know there is a lot you can do with taxonomies which you may not know? In this talk I will outline what taxonomies are and how you can create your own custom taxonomies. I will share some use cases for having custom taxonomies as well as some different and interesting ways in which I have used taxonomies on some sites in the past. In addition I will looks at some of the ways in which you can optimise the admin UI for taxonomies, making it more appropriate in some cases. The talk will also look at how you can associate data with different taxonomy terms.

Mark Wilkinson

October 22, 2016
Tweet

More Decks by Mark Wilkinson

Other Decks in Technology

Transcript

  1. Tremendous Taxonomies
    WordCamp Manchester 2016
    @wpmark
    markwilkinson.me
    highrise.digital

    View Slide

  2. “a classification into
    ordered categories”
    dictionary.com

    View Slide

  3. Grouping & Organising Content

    View Slide

  4. Default
    Taxonomies
    Categories / category
    Tags / post_tag
    Link Category / link_category
    Post Formats / post_format
    Menus / nav_menu

    View Slide

  5. Custom
    Taxonomies

    View Slide

  6. function hd_register_tax() {
    register_taxonomy(
    $taxonomy,
    $objects,
    $args
    );
    }
    add_action( ‘init’, ‘hd_register_tax’ );

    View Slide

  7. labels
    show_ui
    show_in_menu
    hierarchical
    meta_box_cb

    View Slide

  8. hierarchical
    true false

    View Slide

  9. meta_box_cb
    https://github.com/highrisedigital/highrise-taxonomy-ui
    https://github.com/johnbillion/extended-taxos

    View Slide

  10. wp_query
    https://10up.github.io/Engineering-Best-Practices/php/

    View Slide

  11. $news = new WP_Query(
    array(
    ‘post_type’ => ‘post’,
    ‘posts_per_page => -1,
    ‘meta_query’=> array(
    array(
    ‘key’ => ‘home_slider’,
    ‘value’ => ‘yes’
    )
    )
    )
    );

    View Slide

  12. $news = new WP_Query(
    array(
    ‘post_type’=> ‘post’,
    ‘posts_per_page => 100,
    ‘tax_query’ => array(
    array(
    ‘tax’=> ‘locations’,
    ‘field’=> ‘slug’,
    ‘terms’=> ‘home-slider’
    )
    )
    )
    );

    View Slide

  13. View Slide

  14. Bookmarks
    https://www.flickr.com/photos/roxeteer/4326217872

    View Slide

  15. User Bookmarks
    Logged in users can bookmark posts
    The are saved into a bookmarks page for each user
    Bookmarked posts can be removed from this page

    View Slide

  16. user_register
    wp_insert_term()
    wp_set_object_terms()
    wp_set_object_terms()

    View Slide

  17. add_action( 'user_register',
    hd_add_user_bk_term );
    function hd_add_user_bk_term( $user_id ) {
    wp_insert_term(
    'user-' . $user_id,
    'hd_bookmark',
    $args // term desc etc.
    );
    }

    View Slide

  18. $post_id = absint( $_POST[ 'postid' ] );
    $user_id = absint( $_POST[ 'userid' ] );
    $term = get_term_by( 'slug', 'user-' .
    $user_id, 'hd_bookmark' );
    if( $term != false ) {
    wp_set_object_terms( $post_id,
    $term>term_id, 'hd_bookmark', true );
    }

    View Slide

  19. $post_id = absint( $_POST[ 'postid' ] );
    $user_id = absint( $_POST[ 'userid' ] );
    $term = get_term_by( 'slug', 'user-' .
    $user_id, 'hd_bookmark' );
    if( $term != false ) {
    wp_remove_object_terms( $post_id, $term-
    >term_id, ‘hd_bookmark );
    }

    View Slide

  20. Term Meta
    http://themehybrid.com/weblog/introduction-to-wordpress-term-meta

    View Slide

  21. https://wordpress.org/plugins/wp-term-icons/

    View Slide

  22. https://wordpress.org/plugins/wp-term-colors/

    View Slide

  23. {$tax}_edit_form_fields
    {$tax}_add_form_fields
    edit_{$tax}
    create_{$tax}

    View Slide

  24. {$tax}_add_form_fields

    View Slide

  25. {$tax}_edit_form_fields

    View Slide

  26. Not Just Post Types
    User Taxonomies

    View Slide

  27. View Slide

  28. WordPress Development
    WordCamp Manchester 2016 Bronze Sponsors

    View Slide

  29. Thank you
    & Questions?
    Twitter: @wpmark
    WordPress: @wpmarkuk
    markwilkinson.me
    highrise.digital

    View Slide