Displaying Meta Box for Categories - wordpress

I am trying to just display the wordpress categories in a metabox on a different page, the page is custom.
Like a blank page and I am trying to add the wordpress metabox on that page, and be able to add new categories.
Screencast example of what I want to achieve.

It's very simple, just call the following function
add_action( 'init', 'add_cat_meta' );
function add_cat_meta()
{
register_taxonomy_for_object_type( 'category', 'custom_page' );
}
Notice the custom_page, replace custom_page with your original custom post name, for example if you have following to register a custom post
register_post_type( 'restaurant',
array(...);
);
Now to add the category meta box for that custom post you can use
register_taxonomy_for_object_type( 'category', 'restaurant' );
Also, if you want to add the category meta box in the page section then you can use
register_taxonomy_for_object_type('category','page');
But pages are different than posts and don't require a category.

Related

Create custom taxonomy from another custom post type title

I want to automatically create custom taxonomy items from each of the title of an another custom post type's posts whenever a post is created (and delete when deleted).
So i have CPT A and CPT B. If i create a new post in CPT A, i want to add the post title as a taxonomy item of CPT B.
I got this far, but unfortunately it doesn't work properly:
add_action( 'publish_{post_type_name}', 'add_hotel_term' );
function add_hotel_term( $post_ID ) {
$post = get_post( $post_ID ); // get post object
wp_insert_term( $post->post_title, 'custom-taxonomy' );
}
Can you help me on how to proceed?

Elementor Post Widget - get posts from another (multisite) blog id

how to get posts in the elementor pro post widget from another blog-id? i tried with the custom query filter..
add_action( 'elementor/query/my_custom_filter', function( $query ) {
switch_to_blog(3);
$query->set( 'post_type', [ 'post'] );
} );
it works.. it shows me the posts from blog-id 3
BUT.. but all following widgets now also show posts from blog id 3!!! (including header, logo,..)
how is it possible to stop the query after the custom query filter.. like "restore_current_blog();" in this action?

Add a new block to Woocommerce admin order page

I want to create a custom table block in Woocommerce admin order page.
As you can see in a screenshot, I have used :
add_action( 'woocommerce_admin_order_data_after_order_details', 'vp_admin_order_table' );
What I want is to create a separate block, with this table inside it.
Is there any action to trigger gap between Order details and Products list?
Try this code:
function op_register_menu_meta_box() {
add_meta_box(
'Some identifier of your custom box',
esc_html__( 'Box Title', 'text-domain' ),
'render_meta_box',
'shop_order', // shop_order is the post type of the admin order page
'normal', // change to 'side' to move box to side column
'low' // priority (where on page to put the box)
);
}
add_action( 'add_meta_boxes', 'op_register_menu_meta_box' );
function render_meta_box() {
// Metabox content
echo '<strong>Your awesome content goes here</strong>';
}
Remember to set to box as visible under "Screen Options" on the order page.
Further reading:
https://developer.wordpress.org/reference/functions/add_meta_box/
https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes

How to add categories for pages in WP?

Is there any way to add categories for each Page. I am asking it because I see categories option only for posts.
You Can Add the following code in functions.php in your theme folder:
function myplugin_settings() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');
}
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );
I think plugin Add Category to Pages will help you to add categories with pages.

Get Category list related to current page in wordpress

i need to display the category list related to the current page (based on selected category in admin.) in wordpress site. i tried with get_the_category($post_id). But it's not working. Thanks.
You can try get_the_terms() instead. It should return an array of cats. Wordpress considers categories a taxonomy, so specifying 'category' as the taxonomy type should do the trick.Hope this helps.
$array_of_cats = get_the_terms( $post->ID, 'category' );
foreach($array_of_cats as $key=> $value){
//output as a list...
}
Function reference here.

Resources