Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
150
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
200
Why and how to do a Site Health Check
missjwo
0
82
Getting ready for PHP7.2
missjwo
0
2.5k
is it possible to build a truly diverse community?
missjwo
0
370
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
100
Building an Accessible Community - AlterConf Dublin Edition
missjwo
0
110
Building an Accessible Community
missjwo
2
230
Other Decks in Technology
See All in Technology
react-callを使ってダイヤログをいろんなとこで再利用しよう!
shinaps
1
240
Language Update: Java
skrb
2
300
機械学習を扱うプラットフォーム開発と運用事例
lycorptech_jp
PRO
0
250
DroidKaigi 2025 Androidエンジニアとしてのキャリア
mhidaka
2
310
下手な強制、ダメ!絶対! 「ガードレール」を「檻」にさせない"ガバナンス"の取り方とは?
tsukaman
2
450
Evolución del razonamiento matemático de GPT-4.1 a GPT-5 - Data Aventura Summit 2025 & VSCode DevDays
lauchacarro
0
200
CDK CLIで使ってたあの機能、CDK Toolkit Libraryではどうやるの?
smt7174
4
180
AWSを利用する上で知っておきたい名前解決のはなし(10分版)
nagisa53
10
3.1k
「Linux」という言葉が指すもの
sat
PRO
4
140
まずはマネコンでちゃちゃっと作ってから、それをCDKにしてみよか。
yamada_r
2
110
Android Audio: Beyond Winning On It
atsushieno
0
850
バイブスに「型」を!Kent Beckに学ぶ、AI時代のテスト駆動開発
amixedcolor
2
560
Featured
See All Featured
How to Ace a Technical Interview
jacobian
279
23k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.6k
Automating Front-end Workflow
addyosmani
1370
200k
GraphQLとの向き合い方2022年版
quramy
49
14k
The Invisible Side of Design
smashingmag
301
51k
The Cost Of JavaScript in 2023
addyosmani
53
8.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
We Have a Design System, Now What?
morganepeng
53
7.8k
How to train your dragon (web standard)
notwaldorf
96
6.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
840
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