How to disable Default Post Category in WordPress? - wordpress

I'm creating a new post without selecting category but whenever new post is created at that time it automatically selects category. I want to disable auto select category.
Can anyone help me?

The best way of removing default category is to change a parameter is to change the parameter 'default_category' in your 'wp_options' table.
Set the parameter 'default_category' to 0. Note that it won't change the category displayed in your dashboard in the Writing settings, but it will prevent Wordpress to set a default term to your post.
Wordpress doesn't require a term or category to be set by default. It's just not true ;)

Every single post created in Wordpress will be assigned to a category by default. Even if you don't create categories, Posts will be assigned to a default category called "Uncategorized". This is how the Wordpress system works.
First of all, can you explain to us why you don't want to assign categories?
is this because you don't want the category slug added to the post URL? If this is the reason, there are so few ways to achieve this.
This plugin removes the CATEGORY slug from the post URL.
https://wordpress.org/plugins/remove-category-url/
Few other references to removing Category slug from URL without plugins:
https://jonnyjordan.com/blog/how-to-remove-category-from-your-urls-in-wordpress/
Remove category & tag base from WordPress url - without a plugin
Wordpress post will have 1 CATEGORY. This is how Wordpress works. Maybe if you want to remove the default category but if you want to select a category manually then we can achieve that with some custom coding. But you cannot have a WordPress post without assigning a category to it.
The below code will remove the default category when another category is set to a post. Make sure you have set the default category as Uncategorized.
//remove default category (uncategorized) when another category has been set
function remove_default_category($ID, $post) {
//get all categories for the post
$categories = wp_get_object_terms($ID, 'category');
//if there is more than one category set, check to see if one of them is the default
if (count($categories) > 1) {
foreach ($categories as $key => $category) {
//if category is the default, then remove it
if ($category->name == "Uncategorized") {
wp_remove_object_terms($ID, 'uncategorized', 'category');
}
}
}
}
//hook in to the publsh_post action to run when a post is published
add_action('publish_post', 'remove_default_category', 10, 2);
Do let me know if this helps.

Related

Make Custom Post Type with custom field inherit category's custom field when empty

been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

WordPress - Custom page order for next_post_link within custom taxonomy

next_post_link as far as I'm aware, just gets the next post within the taxonomy based on date of creation. I was wondering if I could use custom fields to have it select the next post by the order in there. Like a page number field and it grabs the next post within the taxonomy with the next page number.
So we're in Taxonomy 'x' on page 2 (created 2 weeks ago), and want to move to page 3 (created today), and there's a page 4 (created 1 week ago).
It would go to page 4, but I want it to go to page 3.
Is there a good way to do this?
If you use custom fields like you mentioned, it wouldn't be using a taxonomy. You would be ordering the posts, or getting the posts, based on the get_field() function for acf.
$posts = get_posts();
foreach ($posts as $post) {
// This gets the custom field from ACF
$post->order = get_field('name-of-field', $post->ID);
if ($post->order == whatever_number_you_want) {
$active_post = $post;
}
}
echo $active_post;

Add specific words to WordPress custom type slug

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??
You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.
You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).
To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

How to Link Sub Taxonomy to products in wordpress

I have created custom post types called Internal products
I Have a page page-internal-products.php which list all the custom taxonomy for the Custom post type Internal product
On clicking on the taxonomies takes me to a page which lists the sub taxonomies for the particular parent taxonomy for which i have created the page called taxonomy-internalproducts_categories.php
On clicking on the sub taxonomy. I need to go to a page which lists all the products for this sub taxonomy. How can I achieve this?
You can access the currently queried object with the get_queried_object() function and then check to see if the category has a parent or not. If it has - display posts in it, if it doesn't display all categories belonging to this category.
Here is an example code to do that:
$category = get_queried_object();
if ( $category->parent ) {
// This is a sub-category
get_template_part( 'internal-products', 'list' );
} else {
// This is a main category
get_template_part( 'internal-products', 'categories-list' );
}
What this code will do is that it will include a theme file called internal-products-list.php or internal-products.php if the current category is a sub-category. Otherwise it will include either internal-products-categories-list.php or internal-products.php.
You can of course just write all of your code within the if/else blocks - that's up to you.

Resources