WP-Mix

A fresh mix of code snippets and tutorials

WordPress Functions for Theme Testing

Some quick copy/paste snippets taken from my dev/test theme, Introspection. Posting them here for reference so I can grab ’em as needed.

Add Custom Post Type (Book):

// Add Custom Post Type (Book)
function shapeSpace_add_custom_post_type_book() {
	
	$labels = array(
		'name'               => _x('Books', 'post type general name', 'shapeSpace'),
		'singular_name'      => _x('Book', 'post type singular name', 'shapeSpace'),
		'menu_name'          => _x('Books', 'admin menu', 'shapeSpace'),
		'name_admin_bar'     => _x('Book', 'add new on admin bar', 'shapeSpace'),
		'add_new'            => _x('Add New', 'book', 'shapeSpace'),
		'add_new_item'       => __('Add New Book', 'shapeSpace'),
		'new_item'           => __('New Book', 'shapeSpace'),
		'edit_item'          => __('Edit Book', 'shapeSpace'),
		'view_item'          => __('View Book', 'shapeSpace'),
		'all_items'          => __('All Books', 'shapeSpace'),
		'search_items'       => __('Search Books', 'shapeSpace'),
		'parent_item_colon'  => __('Parent Books:', 'shapeSpace'),
		'not_found'          => __('No books found.', 'shapeSpace'),
		'not_found_in_trash' => __('No books found in Trash.', 'shapeSpace'),
	);
	
	$args = array(
		'labels'             => $labels,
		'taxonomies'         => array('category', 'people', 'post_tag'),
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array('slug' => 'book'),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'show_in_rest'       => true,
		'supports'           => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'post-formats')
	);
	
	register_post_type('book', $args);
	
}
add_action('init', 'shapeSpace_add_custom_post_type_book');

Add Custom Taxonomy (People):

// Add Custom Taxonomy (People)
function shapeSpace_add_custom_taxonomy_people() {
	
	register_taxonomy(
		'people',
		array('post','usp_post','book'),
		array(
			'label' => __('People'),
			'rewrite' => array('slug' => 'person'),
			'hierarchical' => true,
		)
	);
	
}
add_action('init', 'shapeSpace_add_custom_taxonomy_people');

Add Custom Taxonomy (Animals):

// Add Custom Taxonomy (Animals)
function shapeSpace_add_custom_taxonomy_animals() {
	
	register_taxonomy(
		'animals',
		array('post','usp_post','book'),
		array(
			'label' => __('Animals'),
			'rewrite' => array('slug' => 'animal'),
			'hierarchical' => true,
		)
	);
	
}
add_action('init', 'shapeSpace_add_custom_taxonomy_animals');

Modify main WP query:

// Modify Main Query
function shapeSpace_modify_query($query) {
	
	if ($query->is_main_query() && is_home()) {
		
		$meta_query = array(
			array(
				'key'     => 'my-mood',
				'value'   => 'happy',
				'compare' => 'not like'
			)
		);
		
		$query->set('meta_query', $meta_query);
		$query->set('posts_per_page', '3');
		
	}
	
}
add_action('pre_get_posts', 'shapeSpace_modify_query');

Maybe will be adding more snippets to this post as needed.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress