I am trying to delete data from another table using before_delete_post. However, my script does not work. My before_delete_post is at the top of the code. The effect I would like to get when deleting a post would be to delete the data from another table(wp_used_cars) with the same id.
add_action( 'before_delete_post', 'my_func' );
function my_func( $postid ){
global $post_type;
if ( $post_type != 'my_custom_post_type' ) return;
$post_type->query( $wpdb->prepare( 'DELETE FROM wp_used_cars WHERE post_id = %d', $postid ) );
}
// -------------------------------------------------------------------------
// add custom posts --------------------------------------------------------
// add uzywane custom post and set custom capabilities
$uzywane = new CustomPost('uzywane');
$uzywane->make('Uzywane', 'Uzywane', 'Uzywane', array(
'capabilities' => array(
'edit_post' => 'edit_used_car',
'read_post' => 'read_used_car',
'delete_post' => 'delete_used_car',
'edit_posts' => 'edit_used_cars',
'edit_others_posts' => 'edit_others_used_cars',
'publish_posts' => 'publish_used_cars',
'read_private_posts' => 'read_private_used_cars',
),
));
// add this custom capabilities to admin
$role = get_role( 'administrator' );
$role->add_cap( 'edit_used_car', true );
$role->add_cap( 'read_used_car', true );
$role->add_cap( 'delete_used_car', true );
$role->add_cap( 'edit_used_cars', true );
$role->add_cap( 'edit_others_used_cars', true );
$role->add_cap( 'publish_used_cars', true );
$role->add_cap( 'read_private_used_cars', true );
// if role uzywane don't exists add it and set capabilities to edit uzywane
if( ! role_exists( 'uzywane' ) ) {
add_role(
'uzywane',
__( 'Używane' ),
array(
'read' => true,
'edit_used_car' => true,
'read_used_car' => true,
'delete_used_car' => true,
'edit_used_cars' => true,
'edit_others_used_cars' => true,
'publish_used_cars' => true,
'read_private_used_cars'=> true,
'unfiltered_upload' => true,
'upload_files' => true,
'edit_posts' => true,
)
);
}
// if user has role uzywane than remove links from admin panel
$user = wp_get_current_user();
if ( in_array( 'uzywane', (array) $user->roles ) ) {
function remove_links() {
echo '<style>.menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag {display: none;}</style>';
}
add_action('admin_head', 'remove_links');
}
// if role uzywane don't exists add it and set capabilities to edit uzywane
if( ! role_exists( 'uzywane-admin' ) ) {
add_role(
'uzywane-admin',
__( 'Używane-admin' ),
array(
'read' => true,
'edit_used_car' => true,
'read_used_car' => true,
'delete_used_car' => true,
'edit_used_cars' => true,
'edit_others_used_cars' => true,
'publish_used_cars' => true,
'read_private_used_cars'=> true,
'unfiltered_upload' => true,
'upload_files' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'manage_options' => true,
)
);
}
// if user has role uzywane than remove links from admin panel
$user = wp_get_current_user();
if ( in_array( 'uzywane-admin', (array) $user->roles ) ) {
function remove_links() {
echo '<style> #toplevel_page_revslider, #toplevel_page_revslider, #menu-settings, #toplevel_page_wpseo_dashboard, #toplevel_page_edit-post_type-search-filter-widget, #toplevel_page_easy-responsive-tabs, .menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag, #toplevel_page_edit-post_type-acf-field-group .wp-submenu li, #menu-appearance, .acf-columns-2 .wp-list-table.widefat tr, #acf-field-group-locations, #mymetabox_revslider_0, #acf-field-group-options {display: none;} .wp-list-table.widefat tr#post-456 {display: block };</style>';
}
add_action('admin_head', 'remove_links');
}
function cat_for_used_cars() {
$labels = array(
'name' => _x( 'Kategorie samochodów', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Kategoria samochodów', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Kategoria samochodów', 'text_domain' ),
'all_items' => __( 'Wszystkie samochodów', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'Nazwa', 'text_domain' ),
'add_new_item' => __( 'Dodaj', 'text_domain' ),
'edit_item' => __( 'Edycja', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'rewrite' => false,
);
register_taxonomy( 'usedcars', array( 'uzywane' ), $args );
}
add_action( 'init', 'cat_for_used_cars', 0 );
Try Below code :
add_action( 'before_delete_post', 'my_func' );
function my_func( $postid )
{
global $post_type;
if ( $post_type != 'uzywane' )
return;
global $wpdb;
$prefix = $wpdb->prefix;
$tbl = $prefix.'used_cars';
$used_cars_count = $wpdb->get_var( "SELECT COUNT(*) FROM $tbl where post_id = ". $postid );
if($used_cars_count > 0)
{
$wpdb->delete( $tbl, array( 'post_id' => $postid ) );
}
}
Hope this will help you!
Related
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
I am stuck in a problem assigning custom capability with custom post type to a custom role.
The issue is i want to remove the Add New ( Not with a CSS hack or by unsetting menu item ) option of my custom post type. I have already come across answers that suggest many solutions but none of them works perfectly.
Closest to what i want is this :
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => 'do_not_allow', // false < WP 4.5, credit #Ewout
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
This code removes the Add New link but it assigns the default post slug for capabilities.Notice line 2 of above code. If i change it to my custom post type slug, it stops working and i can't even go posts page.
One thing to keep in mind is that i am working with custom role, which has only read capability by default.
If i also assign the edit_posts capability then my objective is achieved, but then user can also access posts and comments, which i do not want.
Woocommerce is doing this. I dive into woocommerce code and add this line where it registers its products and orders.
'capabilities' => array( 'create_posts' => 'do_not_allow' ),
And everything works as i want. I have explored woocommerce code all day but can't find how it is doing this. Can someone else help me out on this with another pair of eyes ? :)
Much appreciated. Thanks..
I already mention to you that give us little more details about the plan that you want to do?
Here is the solution that I think it would be helpful for you.
If you want to used map_meta_cap => true then you will need to change capability_type as well with your own type, see more https://codex.wordpress.org/Function_Reference/register_post_type#capability_type
But if you set your own capability_type value then you will need to add capability to users role those you want to add.
here is a complete example.
if ( !class_exists( 'WPSE64458_CPT_Register' ) ) {
class WPSE64458_CPT_Register {
function __construct() {
add_action( 'init', array( $this, 'wpse64458_register_post_types' ), 5 );
add_action( 'admin_init', array( $this, 'wpse64458_add_caps' ), 5 );
add_filter( 'register_post_type_args', array( $this, 'wpse64458_modify_cpt_registry' ) , 10, 2 );
}
/**
* Register core post types.
*/
public function wpse64458_register_post_types() {
if ( ! is_blog_installed() || post_type_exists( 'member' ) ) {
return;
}
register_post_type( 'member',
apply_filters( 'wpse64458_callb_post_type_member',
array(
'labels' => array(
'name' => __( 'Members', 'domaintext' ),
'singular_name' => __( 'Member', 'domaintext' ),
'all_items' => __( 'All Members', 'domaintext' ),
'menu_name' => _x( 'Members', 'Admin menu name', 'domaintext' ),
'add_new' => __( 'Add New', 'domaintext' ),
'add_new_item' => __( 'Add new member', 'domaintext' ),
'edit' => __( 'Edit', 'domaintext' ),
'edit_item' => __( 'Edit member', 'domaintext' ),
'new_item' => __( 'New member', 'domaintext' ),
'view' => __( 'View member', 'domaintext' ),
'view_item' => __( 'View member', 'domaintext' ),
'search_items' => __( 'Search members', 'domaintext' ),
'not_found' => __( 'No members found', 'domaintext' ),
'not_found_in_trash' => __( 'No members found in trash', 'domaintext' ),
'parent' => __( 'Parent member', 'domaintext' ),
'featured_image' => __( 'Member image', 'domaintext' ),
'set_featured_image' => __( 'Set member image', 'domaintext' ),
'remove_featured_image' => __( 'Remove member image', 'domaintext' ),
'use_featured_image' => __( 'Use as member image', 'domaintext' ),
'insert_into_item' => __( 'Insert into member', 'domaintext' ),
'uploaded_to_this_item' => __( 'Uploaded to this member', 'domaintext' ),
'filter_items_list' => __( 'Filter members', 'domaintext' ),
'items_list_navigation' => __( 'Members navigation', 'domaintext' ),
'items_list' => __( 'Members list', 'domaintext' ),
),
'public' => true,
'show_ui' => true,
'capability_type' => 'member',
'map_meta_cap' => true,
'menu_icon' => 'dashicons-groups',
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
'rewrite' => array( 'slug' => 'member' ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'has_archive' => 'members',
'show_in_nav_menus' => true,
'show_in_rest' => true,
)
)
);
} // end of wpse64458_register_post_types
/**
* Get capabilities.
*
* #return array
*/
private function wpse64458_set_caps() {
$capabilities = array();
$capability_types = array( 'member' );
foreach ( $capability_types as $capability_type ) {
$capabilities[ $capability_type ] = array(
// Post type
"edit_{$capability_type}",
"read_{$capability_type}",
"delete_{$capability_type}",
"edit_{$capability_type}s",
"edit_others_{$capability_type}s",
"publish_{$capability_type}s",
"read_private_{$capability_type}s",
"delete_{$capability_type}s",
"delete_private_{$capability_type}s",
"delete_published_{$capability_type}s",
"delete_others_{$capability_type}s",
"edit_private_{$capability_type}s",
"edit_published_{$capability_type}s",
// Terms
// "manage_{$capability_type}_terms",
// "edit_{$capability_type}_terms",
// "delete_{$capability_type}_terms",
// "assign_{$capability_type}_terms",
);
}
return $capabilities;
}
/*
Add Capability
*/
public function wpse64458_add_caps(){
global $wp_roles;
if ( ! class_exists( 'WP_Roles' ) ) {
return;
}
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
$capabilities = $this->wpse64458_set_caps();
foreach ( $capabilities as $cap_group ) {
foreach ( $cap_group as $cap ) {
$wp_roles->add_cap( 'editor', $cap );
$wp_roles->add_cap( 'administrator', $cap );
}
}
}
public function wpse64458_modify_cpt_registry( $args, $post_type ){
// Do not filter any other post type
if ( 'member' !== $post_type ) {
// Give other post_types their original arguments
return $args;
}
if( current_user_can('editor') ) {
$args['capabilities']['create_posts'] = false;
}
// Give the custom-css-js post type it's arguments
return $args;
}
}
}
then inherite the class with new WPSE64458_CPT_Register();
With this example I disable editor role to add new member post functionality. modify whatever you like, or you can do this others as well. but you previously mention that you already tried to follow WooCommerce, actually they also do this way,
Hope it make sense to you.
HappyCodding!
You can restrict specific user role to custom post by below script :
Add custom Role
add_action('init','add_my_custom_role');
function add_my_custom_role() {
add_role('my_custom_role',
'Custom Role',
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => false,
'publish_posts' => false,
'create_posts' => false,
)
);
}
Register Custom post
add_action( 'init', 'my_custom_post_type');
function my_custom_post_type() {
$args = array(
'label' => __( 'Custom post', 'custom-text-domain' ),
'description' => __( 'Custom post', 'custom-text-domain' ),
'labels' => $labels,
'supports' => array( 'title', 'comments', 'revisions', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'rewrite' => $rewrite,
'capability_type' => array('custom_post','custom_post'),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
);
register_post_type( 'custom_post', $args );
}
Allow user capabilities for post on the basis of roles
add_action('admin_init','custom_post_add_role_caps',999);
function custom_post_add_role_caps() {
// Add the roles you'd like to administer the custom post types
$roles = array('my_custom_role','editor');
// Loop through each role and assign capabilities
foreach($roles as $the_role) {
$role = get_role($the_role);
$role->add_cap( 'read' );
$role->add_cap( 'read_custom_post');
$role->add_cap( 'read_private_custom_post' );
$role->add_cap( 'edit_custom_post' );
$role->add_cap( 'edit_custom_post' );
$role->add_cap( 'edit_others_custom_post' );
$role->add_cap( 'edit_published_custom_post' );
$role->add_cap( 'publish_custom_post' );
$role->add_cap( 'delete_others_custom_post' );
$role->add_cap( 'delete_private_custom_post' );
$role->add_cap( 'delete_published_custom_post' );
}
}
I hope this will help you, to get more help please visit
https://codex.wordpress.org/Function_Reference/register_post_type
and
https://codex.wordpress.org/Roles_and_Capabilities
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.
I am using the WP REST API plugin and Jetpack plugin for wordpress webservice. So, initially 'Vehicle' is my custom post type where it includes two types
of taxonomies i.e two-wheeler and four-wheeler. So, currently I am not getting the taxonomies in webservice result ie.
https://public-api.wordpress.com/rest/v1.1/sites/site_url/posts/?type=vehicle&status=publish&number=10&offset=0.
Following is my code.
/***************************************************
* Register a two-wheeler, four-wheeler taxonomy with Vehicle post type, with REST API support
***************************************************/
add_action( 'init', 'vehicle_group_taxonomy', 30 );
function vehicle_group_taxonomy() {
$labels = array(
'name' => _x( 'two_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'two_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search two_wheeler' ),
'all_items' => __( 'All two_wheeler' ),
'parent_item' => __( 'Parent two_wheeler' ),
'parent_item_colon' => __( 'Parent two_wheeler:' ),
'edit_item' => __( 'Edit two_wheeler' ),
'update_item' => __( 'Update two_wheeler' ),
'add_new_item' => __( 'Add New two_wheeler' ),
'new_item_name' => __( 'New two_wheeler Name' ),
'menu_name' => __( 'two_wheeler' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'two_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'two_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'two_wheeler', array( 'vehicle' ), $args );
$labels_speciality = array(
'name' => _x( 'four_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'four_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search four_wheeler' ),
'all_items' => __( 'All four_wheeler' ),
'parent_item' => __( 'Parent four_wheeler' ),
'parent_item_colon' => __( 'Parent four_wheeler:' ),
'edit_item' => __( 'Edit four_wheeler' ),
'update_item' => __( 'Update four_wheeler' ),
'add_new_item' => __( 'Add New four_wheeler' ),
'new_item_name' => __( 'New four_wheeler Name' ),
'menu_name' => __( 'four_wheeler' ),
);
$args_speciality = array(
'hierarchical' => true,
'labels' => $labels_speciality,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'four_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'four_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'four_wheeler', array( 'vehicle' ), $args_speciality );
}
/********************************************************
* Add REST API support to an already registered post type.
*********************************************************/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'vehicle';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
/*******************************************************
* Add REST API support to an already registered taxonomy.
*********************************************************/
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
//be sure to set this to the name of your taxonomy!
$taxonomy_name = array('two_wheeler','four_wheeler');
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
}
}
I've looked over several posts about adding custom taxonomies to CPT columns; I'm able to get everything working except actually displaying said taxonomies (publication). Here's my CPT code:
add_action( 'init', 'pb_custom_post_type' );
function pb_custom_post_type() {
$labels = array(
'name' => _x( 'Press', 'post type general name' ),
'singular_name' => _x( 'Press', 'post type singular name' ),
'add_new' => _x( 'Add New', 'review' ),
'add_new_item' => __( 'Add New Press' ),
'edit_item' => __( 'Edit Press' ),
'new_item' => __( 'New Press' ),
'all_items' => __( 'All Press' ),
'view_item' => __( 'View Press' ),
'search_items' => __( 'Search Press' ),
'not_found' => __( 'No press found' ),
'not_found_in_trash' => __( 'No press found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Press'
);
$args = array(
'labels' => $labels,
'description' => 'Press information',
'public' => true,
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'has_archive' => true,
);
register_post_type( 'press', $args );
}
add_filter( 'manage_edit-press_columns', 'my_edit_press_columns' ) ;
function my_edit_press_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title' ),
'publication' => __( 'Publication' ),
'date' => __( 'Date' )
);
return $columns;
}
Custom Taxonomy
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
}
Now I've seen two different choices, either trying to add show_ui/show_admin_column in the taxonomy arguments, or having another function with switch statements. I've tried both below, is there something crucial I'm missing?
1
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
$args = array (
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ), $args );
}
2
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "publication":
echo get_post_meta( $post_id, 'publication', true);
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
Remove your custom columns function, and add 'show_admin_column' => true to the actual array of args for register_taxonomy:
register_taxonomy( 'publication', 'press', array( 'show_admin_column' => true, 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
Edit: you might also want to add 'taxonomies' => array( 'publication' ) to the register_post_type args.