I am using A custom Post Type with Advanced Custom Fields to create custom entries of a post. My Customs Post Type and Options are added with (please scroll on codebox to see all):
add_action( 'init', 'register_cpt_campaigns' );
function register_cpt_campaigns() {
$labels = array(
'name' => __( 'Campaigns', 'campaigns' ),
'singular_name' => __( 'campaign', 'campaigns' ),
'add_new' => __( 'Add New', 'campaigns' ),
'add_new_item' => __( 'Add New Campaign', 'campaigns' ),
'edit_item' => __( 'Edit Campaign', 'campaigns' ),
'new_item' => __( 'New Campaign', 'campaigns' ),
'view_item' => __( 'View Campaign', 'campaigns' ),
'search_items' => __( 'Search Campaigns', 'campaigns' ),
'not_found' => __( 'Campaign not found', 'campaigns' ),
'not_found_in_trash' => __( 'Campaign not found', 'campaigns' ),
'parent_item_colon' => __( 'Parent campaign:', 'campaigns' ),
'menu_name' => __( 'Campaigns', 'campaigns' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Custom post type for Discovr Campaigns',
'supports' => array( 'author','title' ),
'taxonomies' => array( 'campaign_category', 'campaign_action' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-widgets-menus',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'campaigns', $args );
}
And custom Taxonomies with (please scroll on codebox to see all):
// Register Campaign Type Taxonomy
function campaign_post_category() {
$labels = array(
'name' => _x( 'Campaign Types', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Campaign Type', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Campaign Types', 'text_domain' ),
'all_items' => __( 'All types', 'text_domain' ),
'parent_item' => __( 'Parent types', 'text_domain' ),
'parent_item_colon' => __( 'Parent types:', 'text_domain' ),
'new_item_name' => __( 'New Campaign Type', 'text_domain' ),
'add_new_item' => __( 'Add New Campaign Type', 'text_domain' ),
'edit_item' => __( 'Edit Campaign Type', 'text_domain' ),
'update_item' => __( 'Update Campaign Type', 'text_domain' ),
'view_item' => __( 'View Campaign Type', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate campaign types with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove campaign types', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular campaign types', 'text_domain' ),
'search_items' => __( 'Search campaign type', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No campaign types', 'text_domain' ),
'items_list' => __( 'Campaign type list', 'text_domain' ),
'items_list_navigation' => __( 'Campaign type list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => false,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
);
register_taxonomy( 'campaign_category', array( 'campaign_categories' ), $args );
}
add_action( 'init', 'campaign_post_category', 0 );
// Register Campaign Status Taxonomy
function campaign_post_action() {
$labels = array(
'name' => _x( 'Campaign Status', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Campaign Status', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Campaign Status', 'text_domain' ),
'all_items' => __( 'All Status', 'text_domain' ),
'parent_item' => __( 'Parent Status', 'text_domain' ),
'parent_item_colon' => __( 'Parent Status:', 'text_domain' ),
'new_item_name' => __( 'New Campaign Status', 'text_domain' ),
'add_new_item' => __( 'Add New Campaign Status', 'text_domain' ),
'edit_item' => __( 'Edit Campaign Status', 'text_domain' ),
'update_item' => __( 'Update Campaign Status', 'text_domain' ),
'view_item' => __( 'View Campaign Status', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate campaign status with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove campaign status', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular campaign status', 'text_domain' ),
'search_items' => __( 'Search campaign status', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No campaign status', 'text_domain' ),
'items_list' => __( 'campaign status list', 'text_domain' ),
'items_list_navigation' => __( 'campaign status list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => false,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
);
register_taxonomy( 'campaign_action', array( 'campaign_actions' ), $args );
}
add_action( 'init', 'campaign_post_action', 0 );
I also have some functions that are added to the theme to support my functionality of the post-type. I have removed these to rectify my issue but did not fix anything.
// Add Default Campaign Status
function set_default_campaign_status( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$defaults = array(
'campaign_action' => array( 'Draft' )
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'set_default_campaign_status', 0, 2 );
// Add Default Campaign Type
function set_default_campaign_type( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$defaults = array(
'campaign_category' => array( 'Not Selected' )
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'set_default_campaign_type', 0, 2 );
// Delete post
function delete_post(){
global $post;
$deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
if (current_user_can('edit_post', $post->ID)) {
echo '<button class="m-t-10 m-b-10 btn btn-danger btn-cons" type="button">Delete</button>';
}
}
//Redirect after delete post in frontend
add_action('trashed_post','trash_redirection_frontend');
function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
wp_redirect( get_option('siteurl').'/campaigns' );
exit;
}
}
// Add default Campaign Pages
function discovr_campaign_endpoints() {
add_rewrite_endpoint( 'overview', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-details', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-audience', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-page', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-ads', EP_PERMALINK );
add_rewrite_endpoint( 'edit-campaign', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
}
add_action( 'init', 'discovr_campaign_endpoints' );
// Update Campaign Page on Save
add_action( 'save_post', 'wpse105926_save_post_callback' );
function wpse105926_save_post_callback( $post_id ) {
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {
// unhook this function to prevent infinite looping
remove_action( 'save_post', 'wpse105926_save_post_callback' );
// update the post slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => '' // do your thing here
));
// re-hook this function
add_action( 'save_post', 'wpse105926_save_post_callback' );
}
}
// Add Campaign Title Label and Instuctions
function my_acf_prepare_field( $field ) {
$field['label'] = "Campaign Title";
$field['instructions'] = "Internal use only. Ex: `Retarget Abandoned Bookings`";
return $field;
}
add_filter('acf/prepare_field/name=_post_title', 'my_acf_prepare_field');
Unfortunately, everytime I save a custom field it reverts to empty or as it's original. This only happens with the ACF fields and not with fields such as title, author or taxonomies not overwritten with ACF. Please see the following gif images to understand further.
Backend of Wordpress with Post being created and updated
ACF Custom Fields Group Settings
Frontend Post being created
I have tried removing all frontend funtionality but still doesn't work in the backend, also removed all plugins either than ACF. No error in debug or firebug. I can only imaging that there is a conflict in the fields, or ACF is not able to find the appropriate place to save these.
Any help is greatly appreciated and I hope that there is something simple that I have missed.
UPDATE
This works by changing the field_name and field_label to something obscure.
ACF Field Group working
Frontend form saving
Getting this to now work is great, however, I need the field_label and field_name to match the purpose of these fields. I could overwrite these within the functions.php but this will be a nightmare as we potentially have 20 fields to add.
If you change the field_label to what is the purpose of this campaign and the field_name to what_is_the_purpose_of_this_campaign it does not work.
However, if you change the field_label` to whatisthepurposeofthiscampaign and the field_name to whatisthepurposeofthiscampaign it does work.
It seems that any spaces break the connection of the field_label and field_name
This error was created by permissions problems with certain plugins. I had uploaded via FTP as root, whereas Wordpress was using www-data as the file owner. I matched the permissions through the console and this has been fixed.
Thank you for your help.
Related
Reading the book http://shop.oreilly.com/product/0636920027508.do and I am on page 511 where we define a custom post type in PHP. Using the code below, its supposed to display the created post type: 'product' on the pages.
Mine is not.
Is that code accurate or am I doing something wrong?
function create_product_post_type() {
$labels = array (
'name' => 'Products', 'singular_name' => 'Product'
);
$args = array (
'labels' => $labels,
'public' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'taxonomies' => array( 'category') );
register_post_type('product', $args );
}
add_action( 'init', 'create_product_post_type' );
function add_product_to_archives( $wp_query ) {
$types_array = array( 'post', 'product');
if( is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
set_query_var( 'post_type', $types_array );
}
}
add_action('pre_get_posts', 'add_product_to_archives');
The book is five years old, and I'm now using the most recent version of WordPress. Do you think this is the issue?
It's possible you're missing some mandatory key for 'register_post_type'. You can check full documentation here: https://developer.wordpress.org/reference/functions/register_post_type/
(I'd recommend you to use Wordpress Codex and Developer Code Reference instead of offline books. Info changes from versions and those are the places to keep it updated):
https://developer.wordpress.org
https://codex.wordpress.org/
Try this code:
if ( ! function_exists('products_post_type') ) {
// Register Custom Post Type
function products_post_type() {
$labels = array(
'name' => _x( 'Products', 'Post Type General Name', 'your_text_domain' ),
'singular_name' => _x( 'Product', 'Post Type Singular Name', 'your_text_domain' ),
'menu_name' => __( 'Products', 'your_text_domain' ),
'name_admin_bar' => __( 'Products', 'your_text_domain' ),
'archives' => __( 'Products Archives', 'your_text_domain' ),
'attributes' => __( 'Products Attributes', 'your_text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'your_text_domain' ),
'all_items' => __( 'All Items', 'your_text_domain' ),
'add_new_item' => __( 'Add New Item', 'your_text_domain' ),
'add_new' => __( 'Add New', 'your_text_domain' ),
'new_item' => __( 'New Item', 'your_text_domain' ),
'edit_item' => __( 'Edit Item', 'your_text_domain' ),
'update_item' => __( 'Update Item', 'your_text_domain' ),
'view_item' => __( 'View Item', 'your_text_domain' ),
'view_items' => __( 'View Items', 'your_text_domain' ),
'search_items' => __( 'Search Item', 'your_text_domain' ),
'not_found' => __( 'Not found', 'your_text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'your_text_domain' ),
'featured_image' => __( 'Featured Image', 'your_text_domain' ),
'set_featured_image' => __( 'Set featured image', 'your_text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'your_text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'your_text_domain' ),
'insert_into_item' => __( 'Insert into item', 'your_text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'your_text_domain' ),
'items_list' => __( 'Items list', 'your_text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'your_text_domain' ),
'filter_items_list' => __( 'Filter items list', 'your_text_domain' ),
);
$args = array(
'label' => __( 'Product', 'your_text_domain' ),
'description' => __( 'Products', 'your_text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'product_category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-products',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => false,
);
register_post_type( 'product', $args );
}
add_action( 'init', 'products_post_type', 0 );
}
You should see a menu item in your Dashboard called Products.
It's possible too that you'll need to create a custom taxonomy for Products categories. Give this a try:
if ( ! function_exists( 'products_category' ) ) {
// Register Custom Taxonomy
function products_category() {
$labels = array(
'name' => _x( 'Product Categories', 'Taxonomy General Name', 'your_text_domain' ),
'singular_name' => _x( 'Product Category', 'Taxonomy Singular Name', 'your_text_domain' ),
'menu_name' => __( 'Category', 'your_text_domain' ),
'all_items' => __( 'All Items', 'your_text_domain' ),
'parent_item' => __( 'Parent Item', 'your_text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'your_text_domain' ),
'new_item_name' => __( 'New Item Name', 'your_text_domain' ),
'add_new_item' => __( 'Add New Item', 'your_text_domain' ),
'edit_item' => __( 'Edit Item', 'your_text_domain' ),
'update_item' => __( 'Update Item', 'your_text_domain' ),
'view_item' => __( 'View Item', 'your_text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'your_text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'your_text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'your_text_domain' ),
'popular_items' => __( 'Popular Items', 'your_text_domain' ),
'search_items' => __( 'Search Items', 'your_text_domain' ),
'not_found' => __( 'Not Found', 'your_text_domain' ),
'no_terms' => __( 'No items', 'your_text_domain' ),
'items_list' => __( 'Items list', 'your_text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'your_text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => false,
);
register_taxonomy( 'product_category', array( 'products' ), $args );
}
add_action( 'init', 'products_category', 0 );
}
I'm creating a plugin and I have a custom post type set up that includes meta boxes, custom fields and custom taxonomies. All that is working fine and on my wordpress dashboard, the "add new post" is all there. However when I try to "save draft" or "publish" a post, it's not working. Error included below.
Here's the code for my CPT.
// Register Custom Post Type
function post_type_book() {
$labels = array(
'name' => _x( 'All Books', 'book post type name', 'author_station' ),
'singular_name' => _x( 'Book', 'Singular book post type name', 'author_station' ),
'menu_name' => __( 'Books', 'author_station' ),
'name_admin_bar' => __( 'Post Type', 'author_station' ),
'archives' => __( 'Item Archives', 'author_station' ),
'attributes' => __( 'Item Attributes', 'author_station' ),
'parent_item_colon' => __( 'Parent Item:', 'author_station' ),
'all_items' => __( 'All Items', 'author_station' ),
'add_new_item' => __( 'Add New Book', 'author_station' ),
'add_new' => __( 'Add New', 'author_station' ),
'new_item' => __( 'New Book', 'author_station' ),
'edit_item' => __( 'Edit Book', 'author_station' ),
'update_item' => __( 'Update Item', 'author_station' ),
'view_item' => __( 'View Book', 'author_station' ),
'view_items' => __( 'View Items', 'author_station' ),
'search_items' => __( 'Search Item', 'author_station' ),
'not_found' => __( 'Not books found', 'author_station' ),
'not_found_in_trash' => __( 'Not books found in Trash', 'author_station' ),
'featured_image' => __( 'Book Cover Image', 'author_station' ),
'set_featured_image' => __( 'Set Book Cover Image', 'author_station' ),
'remove_featured_image' => __( 'Remove Book Cover Image', 'author_station' ),
'use_featured_image' => __( 'Use as Book Cover Image', 'author_station' ),
'insert_into_item' => __( 'Insert into item', 'author_station' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'author_station' ),
'items_list' => __( 'Items list', 'author_station' ),
'items_list_navigation' => __( 'Items list navigation', 'author_station' ),
'filter_items_list' => __( 'Filter items list', 'author_station' ),
);
$args = array(
'label' => __( 'book', 'author_station' ),
'description' => __( 'Custom Book Entry', 'author_station' ),
'supports' => array('title'),
'taxonomies' => array( 'genres', 'series', 'keywords' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'as_book' ),
);
register_post_type( 'as_book', $args );
}
add_action( 'init', 'post_type_book');
function save_as_book( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['your_as_book_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['as_book'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'as_book', true );
$new = $_POST['as_book'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'as_book', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'as_book', $old );
}
}
add_action( 'save_post', 'save_as_book' );
This is the error that comes up when I try to 'save draft' or 'publish' a post.
Fatal error: Call to a member function get_queried_object_id() on null in ..../wp-includes/query.php on line 60
Trying to create a set of user taxonomies with no result. Error log is empty. The code works for custom post types, but when I set it to create user taxobomy- nothing happens. Not sure where this went wrong.
add_action( 'init', 'user_status_taxonomy' );
function user_status_taxonomy() {
register_taxonomy(
'profession', 'user',
array(
'public' => true,
'labels' => array(
'name' => __( 'Professions' ),
'singular_name' => __( 'Profession' ),
'menu_name' => __( 'Professions' ),
'search_items' => __( 'Search Professions' ),
'popular_items' => __( 'Popular Professions' ),
'all_items' => __( 'All Professions' ),
'edit_item' => __( 'Edit Profession' ),
'update_item' => __( 'Update Profession' ),
'add_new_item' => __( 'Add New Profession' ),
'new_item_name' => __( 'New Profession Name' ),
'separate_items_with_commas' => __( 'Separate professions with commas' ),
'add_or_remove_items' => __( 'Add or remove professions' ),
'choose_from_most_used' => __( 'Choose from the most popular professions' ),
)
)
);
}
You will need to add the category page to the admin menu. The function above is simply registering the taxonomy. Not displaying it anywhere until its called upon.
add_action( 'admin_menu', 'add_user_categories_to_admin' );
function add_user_categories_to_admin() {
$taxonomy = get_taxonomy('profession');
add_users_page(
esc_attr( $taxonomy->labels->menu_name ),//The Page Title
esc_attr( $taxonomy->labels->menu_name ),//The Menu Title
$taxonomy->cap->manage_terms, // Taxonomy Capabilities
'edit-tags.php?taxonomy=' . $taxonomy->name // The Slug
);
}
I use a custom post type, which also should have a custom taxonomy. When I visit the archive page /kollektion/ I can see all items of this CPT, when I visit /kollektion/mytaxonomy1/ I can see all items which are assigned to this taxonomy.
When I click on the single item of the custom post type, I get a 404 error. I also tried to go into my WP dashboard and change and resave the permalinks again - with no success.
Does anybody see an error in my declaration which causes the 404 error on single pages of my custom post type?
Thanks.
functions.php: Taxonomy
// register two taxonomies to go with the post type
function sm_register_taxonomy() {
// set up labels
$labels = array(
'name' => 'Kollektionen Categories',
'singular_name' => 'Kollektion Category',
'search_items' => 'Search Kollektion Categories',
'all_items' => 'All Kollektion Categories',
'edit_item' => 'Edit Kollektion Category',
'update_item' => 'Update Kollektion Category',
'add_new_item' => 'Add New Kollektion Category',
'new_item_name' => 'New Kollektion Category',
'menu_name' => 'Kollektion Categories'
);
// register taxonomy
register_taxonomy( 'kollektionen', 'kollektion', array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'kollektion', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
),
) );
}
add_action( 'init', 'sm_register_taxonomy' );
functions.php: Custom post type
// Register Custom Post Type Kollektion
function post_type_kollektion() {
$labels = array(
'name' => _x( 'Kollektionen', 'Post Type General Name', 'genesis' ),
'singular_name' => _x( 'Kollektion', 'Post Type Singular Name', 'genesis' ),
'menu_name' => __( 'Kollektion', 'genesis' ),
'name_admin_bar' => __( 'Kollektion', 'genesis' ),
'parent_item_colon' => __( 'Parent Item:', 'genesis' ),
'all_items' => __( 'All Items', 'genesis' ),
'add_new_item' => __( 'Add New Item', 'genesis' ),
'add_new' => __( 'Add New', 'genesis' ),
'new_item' => __( 'New Item', 'genesis' ),
'edit_item' => __( 'Edit Item', 'genesis' ),
'update_item' => __( 'Update Item', 'genesis' ),
'view_item' => __( 'View Item', 'genesis' ),
'search_items' => __( 'Search Item', 'genesis' ),
'not_found' => __( 'Not found', 'genesis' ),
'not_found_in_trash' => __( 'Not found in Trash', 'genesis' ),
'items_list' => __( 'Items list', 'genesis' ),
'items_list_navigation' => __( 'Items list navigation', 'genesis' ),
'filter_items_list' => __( 'Filter items list', 'genesis' ),
);
$args = array(
'label' => __( 'Kollektion', 'genesis' ),
'description' => __( 'Kollektionen', 'genesis' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
'taxonomies' => array( 'post_tag', 'kollektionen' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'kollektion',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'kollektion', $args );
}
add_action( 'init', 'post_type_kollektion');
}
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'kollektion', 'kollektionen'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
It looks like you have the same slug for taxonomy and custom post type. Try to change slug of taxonomy (or CPT) and resave the permalinks - this should help.
I'm creating a child theme for a client and making some new post types. Everything works great so far, except that the posts in the custom post type are not showing up underneath the categories page; although they are in that category.
Here is my code for creating the custom post type...
// Register Custom Post Type
function directory_listing() {
$labels = array(
'name' => _x( 'Directory Items', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Directory Item', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Directory Listing', 'text_domain' ),
'name_admin_bar' => __( 'Directory', 'text_domain' ),
'parent_item_colon' => __( 'Directory:', 'text_domain' ),
'all_items' => __( 'All Listings', 'text_domain' ),
'add_new_item' => __( 'Add New Listing', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Listing', 'text_domain' ),
'edit_item' => __( 'Edit Listing', 'text_domain' ),
'update_item' => __( 'Update Listing', 'text_domain' ),
'view_item' => __( 'View Listing', 'text_domain' ),
'search_items' => __( 'Search Listing', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'directory_listings', 'text_domain' ),
'description' => __( 'Directory Listing', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-flag',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'directory_listings', $args );
}
// Hook into the 'init' action
add_action( 'init', 'directory_listing', 0 );
}
The part I don't understand is that when I go to the actual post in the custom post type the "view post" url I get is something like...
http://otpguide.dev/directory_listings/another-place/
However, the category url for the category that particular post is marked under is something like...
http://otpguide.dev/category/directory/alpharetta/
Shouldn't the custom post type have a url more like...
http://otpguide.dev/category/directory/alpharetta/another-place ??
Why don't my posts show up under the categories page?
Figured it out!
Custom post types aren't included in archive.php unless you specify them to be. I added this snippet and it worked perfectly.
// allow custom post types in archive
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if( !is_admin() ) {
if ( !is_post_type_archive() && $query->is_archive() ) {
$query->set( 'post_type', array( 'post', 'your-custom-post-type', 'another_custom_post_type' ) );
}
return $query;
}
}
Apparently the same thing is true for search.php; you also have to have ''exclude_from_search' => false,' declared when you register your custom post type
// add custom post types to search
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'feed', 'your-custom-post-type', 'another_custom_post_type'));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'the_search_query', 'searchAll' );