Intermediate to Advanced Wordpress
Registering a custom post type in WordPress
- A different way of categorizing your posts; there's generally pages and posts. Pages have more hierarchical organization scheme; posts are flat hierarchy.
- Creating custom post types for different types of content; you can sub-categorize it, extend it with different fields, etc.
- Also easier for clients: rather than just seeing pages and posts and then using categories to sort them, it gives you a way to keep content well organized.
- A better way to manage content on a site.
- Registering a custom post type: http://codex.wordpress.org/Function_Reference/register_post_type
- Usage: <?php register_post_type( $post_type, $args ) ?>
- You can hide post types both from the front end (site visitors) and from the WP backend.
- You can also limit certain roles' capabilities so that they only have access to certain post types
- http://codex.wordpress.org/Roles_and_Capabilities -- working with roles and capabilities is done via code by default
- Members plugin is useful for managing roles: http://wordpress.org/plugins/members/ -- gives you a UI for roles and capabilities
- Grant's WP class for registering custom post types: https://github.com/creativecoder/custom-post-type-helper -- shortcut for handling lots of labels and other things you need to set for custom post types
- People often register custom post types in the functions.php file of your theme; a bad thing about that is if someone changes their theme, they lose all the post types. Grant liks to put it in a wp-content/mu-plugins. Any PHP files that you drop into that folder will automatically be run by WP core. This is for code you want to run all the time; the only way to turn them off is to delete the files. Then no matter what theme is, that code sticks around. This is core WP functionality. You can just drop each custom post type into its own PHP file in that folder, or one big PHP file, etc.
- You also create (register) taxonomies within code; once you create the taxonomies in code, you'll see them in your WP UI and can add terms there.
- Also: http://wordpress.org/plugins/simple-custom-types/
Relating content types
- E.g. with property and neighborhood post types, you want properties to belong to neighborhoods. No way to relate posts to each other.
- http://wordpress.org/plugins/posts-to-posts/ is a good plugin for that; excellent documentation in github.
- You can also use this to connect users to posts (e.g. to connect multiple authors to one post.)
Advanced custom fields
- break your content apart in sensible ways so it's not just one big block of HTML that's editable by the user.
- Write your code or use the Advanced Custom Fields GUI
- Can also handle post relationships
Custom queries in WP
- You can display customized lists of posts with queries, most recommended way is using a WP_Query object; has all of the methods of a normal query, e.g. to loop through posts.
- Another way: http://codex.wordpress.org/Function_Reference/get_post -- main difference with that and WP_Query, you don't have the associated methods available with WP_Query. Better when you need a list of posts that you're doing something else with. Depends what you're doing with the data.
- Another way: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
- Don't ever use http://codex.wordpress.org/Function_Reference/query_posts!