$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Custom Post Types
Search
Jenny Wong
April 26, 2014
Technology
0
160
Custom Post Types
A introductory guide to WordPress Custom Post Types. Presented at WordCamp Sheffield 2014
Jenny Wong
April 26, 2014
Tweet
Share
More Decks by Jenny Wong
See All by Jenny Wong
The Road Taken
missjwo
0
220
Why and how to do a Site Health Check
missjwo
0
93
Getting ready for PHP7.2
missjwo
0
2.5k
is it possible to build a truly diverse community?
missjwo
0
390
Going Backstage on Community Trends: Bug or Feature?
missjwo
1
250
We Didn’t Care About Diversity — What Happened Next Is Amazing!
missjwo
0
270
Building on WordPress for Enterprise
missjwo
0
110
Building an Accessible Community - AlterConf Dublin Edition
missjwo
0
110
Building an Accessible Community
missjwo
2
240
Other Decks in Technology
See All in Technology
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
240
AI との良い付き合い方を僕らは誰も知らない
asei
0
160
エンジニアリングをやめたくないので問い続ける
estie
2
1.2k
【U/Day Tokyo 2025】Cygames流 最新スマートフォンゲームの技術設計 〜『Shadowverse: Worlds Beyond』におけるアーキテクチャ再設計の挑戦~
cygames
PRO
2
930
文字列の並び順 / Unicode Collation
tmtms
3
620
業務のトイルをバスターせよ 〜AI時代の生存戦略〜
staka121
PRO
2
230
100以上の新規コネクタ提供を可能にしたアーキテクチャ
ooyukioo
0
140
Amazon Quick Suite で始める手軽な AI エージェント
shimy
1
990
S3を正しく理解するための内部構造の読解
nrinetcom
PRO
3
200
AWS re:Invent 2025~初参加の成果と学び~
kubomasataka
0
150
Database イノベーショントークを振り返る/reinvent-2025-database-innovation-talk-recap
emiki
0
240
AWS Security Agentの紹介/introducing-aws-security-agent
tomoki10
0
340
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.1k
What does AI have to do with Human Rights?
axbom
PRO
0
1.9k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
280
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
250
Test your architecture with Archunit
thirion
1
2.1k
Building Adaptive Systems
keathley
44
2.9k
Highjacked: Video Game Concept Design
rkendrick25
PRO
0
240
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Transcript
Hello WC Shef+ield! Jenny Wong : Web Developer : @miss_jwo
Custom Post Types Jenny Wong : Web Developer : @miss_jwo
Fast Talker Feel free to ask me to slow down.
Its good for me.
Content The most important thing about a site.
Post Types WordPress deals with all the different type of
content by de+ining them based on what they are. ! The de+initions are called Post Types.
Post Types ! •Post •Page •Attachments •Revision
•Nav Menus } Defaults
Custom Post Types CPT for short.
Post Types ! •Post •Page •Attachments •Revision
•Nav Menus
Custom Post Types • Post • Page •
Attachments • Revision • Nav Menus •News •Staff … anything from Jen’s head
Why?
Advantages •Hides content from the default query •Management of
content is easier •Improved user interface & usability •The ability to use custom templates
Disadvantages •Performance issue with querying a large table
•If content doesn’t easily +it the post type tables then issues can occur.
Where do CPTs live? The Options
Theme •CPT attached to the theme •Activate Theme &
CPT will appear •Deactivate theme: • User cannot access their CPT data • Data still stored in the database
Plugin • Functionality is separate to the design of
the website •Can activate new theme without functionality disappearing •Plugins can be deactivated
Must Use Plugin mu plugin for short.
Must Use Plugin
Must Use Plugin https://codex.wordpress.org/Must_Use_Plugins
The Code
Register New CPT
Register New CPT <?php ! ! register_post_type( $post_type, $args );!
! $post_type! - Name of post type! $args! ! ! -! An array of arguments.! ! ! ! !
Register New CPT <?php ! ! register_post_type( $post_type, $args );!
! $post_type! - Name of post type! $args! ! ! -! An array of arguments.! ! Full arguments list: ! https://codex.wordpress.org/Function_Reference/register_post_type#Arguments! ! !
CPT in action <?php ! ! function cpt_wcsheff () {!
! ! $args = array( … );! ! ! register_post_type(‘speakers’, $args );! ! }! ! add_action( 'init','cpt_wcsheff');! !
Categories & Tags
Categories & Tags ! Default taxonomy: • category
• post_tag
Categories & Tags function cpt_wcsheff () {! ! …! !
register_post_type(‘speakers’, $args );! ! ! register_taxonomy_for_object_type( ! ! ! 'category', 'speakers' ! ! );! ! ! register_taxonomy_for_object_type( ! ! ! 'post_tag', 'speakers' ! ! );! }!
Custom Taxonomies
Custom Category $post_types = array(‘speakers’);! ! $category_args = array(! !
'hierarchical' => true ! );! ! register_taxonomy( ! ! 'custom_cat', ! ! $post_types,! ! $category_args! );! !
Custom Tag $post_types = array(‘speakers’);! ! $category_args = array(! !
'hierarchical' => false ! );! ! register_taxonomy( ! ! 'custom_tag', ! ! $post_types,! ! $category_args! );! !
Custom Taxonomies https://codex.wordpress.org/ Function_Reference/register_taxonomy
Templating
Custom Templates ! archive-‐$post_type.php archive.php ! single-‐$post_type.php
single.php ! taxonomy-‐$taxonomy-‐$term.php taxonomy-‐$taxonomy.php taxonomy.php
Condition Tags ! is_post_type_hierarchical( $post_type ); ! is_post_type_archive( $post_type
); ! post_type_exists( $post_type ) ; !
Common Issues
Permalinks URL that points to a particular location !
http://codex.wordpress.org/Using_Permalinks
Permalinks Issue: 404 error on a single CPT page
Flushing Permalinks ATTENTION: You should *NEVER EVER* do this on
every page load!!
Flushing in a plugin function cpt_wcsheff() {! register_post_type( ... );!
}! ! ! function my_rewrite_flush() {! cpt_wcsheff();! flush_rewrite_rules();! }! ! ! register_activation_hook( ! ! __FILE__, ! ! 'my_rewrite_flush' ! );! ! !
Flushing in a theme function cpt_wcsheff() {! register_post_type( ... );!
}! ! ! function cpt_wcsheff_rewrite_flush() {! flush_rewrite_rules();! }! ! add_action( ! ! 'after_switch_theme', ! 'cpt_wcsheff_rewrite_flush' ! );! ! !
Permalinks https://codex.wordpress.org/Function_Reference/ register_post_type#Flushing_Rewrite_on_Activation
Slugs
Slugs ! example.com/permalinks/slugs ! example.com/slugs !
CPT Naming
CPT Naming Permalink Structure: ! $custom_post_type =
‘wc_shef+ield’; $custon_tax = ‘wc_shef+ield’; ! example.com/$custom_post_type/post_slug ! example.com/$custom_tax/another_slug
Questions?
Ask me later Jenny Wong : Web Developer : @miss_jwo