Can I add category to my custom taxonomy through some function in wordpress? - wordpress

I use my custom taxonomy on Wordpress project and I need to create new category through code.
Is that even possible? I can`t google some solution for my problem.

Use wp_insert_term
wp_insert_term( 'category_name', 'your_custom_taxonomy' );

Related

WordPress ACF custom taxonomy field display

I'm fairly new to WordPress and using the ACF plugin for the first time. I've created a custom post type with custom fields, two of which are of type taxonomy that are tied to custom taxonomies I set up specifically for this post type. That all works great, but I'd like for the custom taxonomies to only show on the post form and not in the right sidebar since that's redundant and confusing for the editors:
Is there anyway to hide them in the sidebar? I already checked categories and tags under the "hide on screen" option for the custom field group, but that didn't seem to make a difference.
Thank you!
add_action( 'admin_menu' , 'wpdocs_remove_post_custom_fields' );
function wpdocs_remove_post_custom_fields() {
remove_meta_box( 'META_BOX_ID' , 'CUSTOM_POST_TYPE' , 'normal' );
}
add in your active themes file -> functions.php
Following parameters:
META_BOX_ID: https://prnt.sc/3TLMZCHCak17
CUSTOM_POST_TYPE: add you custom post type

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

Is there a way to change the default template for all posts in wordpress?

I am creating posts in wordpress programmatically using php. Is there a way to change the post attributes template either programmatically or using the admin panel for all posts? I would like the default to be Product details as you see in the picture below.
Any help appreciated. Thanks.
after creating post programmatically assign page template.
update_post_meta( $post_id, '_wp_page_template', 'page-template.php' );

wp_insert_post not saving custom taxonomy on devices

I've faced a weird problem on my wordpress website.
I am using the wp_insert_post on front end to save a custom post type and its custom taxonomy. On PC it saves perfectly the custom taxonomy, but when I try to submit on mobile or ipad, then its not saving it.
here's my code:
$post = array(
'post_title'=>$_POST['message_title'],
'post_status'=>'pending',
'post_type'=>'mondd_el',
'tax_input'=>array('me_kategoria'=>$cat_id),
'post_content'=>$_POST['my_message']
);
$postid = wp_insert_post($post);
Thanks
ARE YOU SURE THE CURRENT USER HAVE CAPABILITY ON WORKING WITH TAXONOMY ? PLEASE CHECK THAT... THIS IS WHAT YOU GET FROM WORDPRESS DOCUMENTATION
"tax_input: Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work with a taxonomy, then you must use wp_set_object_terms() instead."

Best method for creating a custom Wordpress post type just like a page

I have a site where I need to add a post type that has all of the exact same features as a normal PAGE in Wordpress but is a post type of "AGENT".
I thought the easiest way to do this would have been to create a normal page with a specific category that I could reference elsewhere in the code...but I know categories are not available on pages.
Is the best way to do this with a custom post type, or is there an easier method?
Thanks!
Well, we can add categories to pages.
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats()
{
register_taxonomy_for_object_type( 'category', 'page' );
}
Or you can create a Custom Taxonomy and assign it to Pages.
Or create the Custom Post Type and configure it as you wish.

Resources