Create automatically a post category in wordpress - wordpress

I want to create automatically a post category with the name of a custom post type post when it will be saved. Is this possible? I searched but found nothing in this direction!
Have a lovely day!

add_action('save_post', 'add_title_as_category');
function add_title_as_category( $postid ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
$post = get_post($postid);
if ( $post->post_type == 'post') { // change 'post' to any cpt you want to target
$term = get_term_by('slug', $post->post_name, 'category');
if ( empty($term) ) {
$add = wp_insert_term( $post->post_title, 'category', array('slug'=> $post->post_name) );
if ( is_array($add) && isset($add['term_id']) ) {
wp_set_object_terms($postid, $add['term_id'], 'category', true );
}
}
}
}

Related

Get Post ID of post you're currently editing in WordPress

I'm attempting to get the ID of the post I am editing in functions.php for the purpose of dynamically rewriting a custom post type slug.
This is what I'm working with so far.
function change_post_type_slug( $args, $post_type ) {
if ( 'custom_post' == $post_type ) {
global $post;
$location = get_field('custom_field', $post->ID);
$args['rewrite']['slug'] = $location;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
I am not sure if the hook register_post_type_args is firing before I am able to get the ID, or if this is even the best way to go about what I am trying to accomplish. Can't find much out there on the subject.
I was able to get it to work with the following:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' == $post_type ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
However it resulted in a notice on the front-end:
Notice: Undefined index: post in /path/to/wordpress/functions.php on line 623
Line 623 is $post_id = $_GET['post'];
You should use the updated_postmeta hook for this, as its run every time you update your custom fields.
Then you can update your post data with wp_update_post() function.
add_action( 'updated_postmeta', function( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'location' === $meta_key ) {
wp_update_post([
'ID' => $object_id,
'post_name' => $meta_value,
]);
}
}, 10, 4 );
Update:
Try this:
function change_post_types_slug( $args, $post_type ) {
if ( 'your-custom_post' === $post_type ) {
// Check and get the custom post ID
$id = isset($_GET[ 'post' ]) ? $_GET[ 'post' ] : '' ;
// $location = get_field('leadpage_location', $id);
$args['rewrite']['slug'] = 'new-slug-here';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
Try this out:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' === $post_type && is_admin() && $_GET['action'] === 'edit' ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
It adds two more conditionals, to check if you're on the admin screen and to check of there is a GET parameter of edit. Probably overkill to do is_admin() as well, but now you're super safe.

WordPress Custom MD5 slug page not found

My "werknemers" post type need an MD5 generated slug to make them unique. In order to do that, I have added the following code:
function isValidMd5($md5 =''){
return preg_match('/^[a-f0-9]{32}$/', $md5);
}
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if(isValidMd5($slug)) { } else {
if ( 'werknemers' == $post_type ) {
$slug = md5( time() );
}
}
return $slug;
}
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
Works perfectly however, the posts are now not accessible, giving a "Page not found" error. Changing the permalinks doesn't help and neither did resetting ".htaccess". I assume I need something specific to be placed in ".htaccess", but I don't know what. Any ideas?
Because I removed the post type slug, the post couldn't be found.
It actually had nothing to do with the MD5 generator.
To fix it, I had to apply the following code (in case anyone else has this problem)
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'werknemers' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'werknemers', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );

Can I turn a WordPress Draft into a published post with a direct link if I know the post ID?

For example, using a function like this:
http://website.com/wp-admin/post.php?post=%%post_id%%&action=publish
P.S. I checked and this does not work, but I'm wondering if there is something similar in spirit that works?
You can paste this code in your theme functions.php file. This will do the trick, now if you change action parameter to draft and sent a get request , it will make that post draft.
add_action( 'admin_init', 'draft_post_status_221' );
function draft_post_status_221(){
// Get current page , so this action will only fire in post.php page.
global $pagenow;
if ( $pagenow != 'post.php' ){
return;
}
$post_id = false;
$action = false;
// get post id
if ( isset($_GET['post']) && !empty($_GET['post']) ){
$post_id = $_GET['post'];
}
// get action
if ( isset($_GET['action']) && !empty($_GET['action']) ){
$action = $_GET['action'];
// for security we only allow draft action
if ( $action != 'draft' ){
$action = false;
}
}
// if $post_id and $action has data than post will be updated.
if ( !empty($post_id) && !empty($action) ){
$args = array(
'ID' => $post_id,
'post_status' => $action
);
wp_update_post( $args );
}
}

Wordpress: remove one category in permaling /%category%/%postname%/

I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").
My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?
Thanks!
You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks
This works for me:
function remove_uncategorized( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'sin-categoria' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );

How to replace the custom post slug by the category name in wordpress

I need to change the permalink struture of my custom post "product"
the default one is Domain_name.com./product/product_name
I need to change it to :
Domain_name.com./category_name/product_name
i used the plugin "Types" to create the custom post
i installed a plugin named wp-permastructure to change the permalink but
when i add %category%/%postname%/ in the new format structure i get a 404 error.
i add the follow code to remove the product slug :
function gp_remove_cpt_slug( $post_link, $post, $leavename )
{
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter( 'product', 'gp_remove_cpt_slug', 10, 3 );
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 3!= count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'product', 'page' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );
But i still get the 404 error
have you some ideas for my problem

Resources