I use the code like below in a submit page.
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_excerpt' => $excerpt,
'post_category' => array($_POST['cat']),
'tags_input' => array($tags),
'post_status' => 'publish',
'post_type' => 'custom_post_type'
);
$pid = wp_insert_post($new_post);
The $_POST['cat'] fetches the correct `category id. My custom taxonomy is named directory.
I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesn't appear.
Is there a way to save the custom category of a custom taxonomy properly.
To assign a taxonomy to a post:
$cat_ids = array( $new_cat_id );
$cat_ids = array_map('intval', $cat_ids);
wp_set_object_terms( $post_id, $cat_ids, 'my_tax_name' );
Related
i used wp_insert_post to add posts to custom post type i had created but i need to insert the posts in category , i used post_category=> array($category_id) but it didn't work .. here is the code
$my_post = array(
'post_type' => 'articles',
'post_title' => "test",
'post_content' => "blabla",
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(38),
);
wp_insert_post( $my_post );
You can set it after the post creation with:
$catid = 38;
wp_set_post_categories($my_post,$catid,false);
If you're not sure what's the category ID you can use the category slug:
$catid = get_category_by_slug( 'xxx' );
The category taxonomy, of course, should already exist in that Custom Post Type.
You can set it in the CPT definition with:
'taxonomies' => array( 'category' ),
so i want to add post programatically with my plugin, i write this code:
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_category' => array(1)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
and this is return post ID to variable but post doesnt show in wp admin posts table. WordPress is counting this like new post but doesnt show. Why?
I have created post type of products & also created taxonomy called productcategory.
Now i need to get particular category in related products.
Somehow i managed to find the taxonomy category id & name by using
$term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids"));
Now how can i get all post of this taxonomy category ?
Show posts associated with certain taxonomy. If specifying a taxonomy registered to a custom post type then instead of using 'category' you would use '{custom_taxonomy_name}'. For instance, if you had a custom taxonomy called "genre" and wanted to only show posts from the "jazz" genre you would use the below code.
<?php
$args = array(
'posts_per_page' => 8,
'orderby' => 'rand',
'post_type' => 'products',
'productcategory' => 'your_cat_name',
'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>
$terms = get_the_terms( $post->ID, 'productcategory' );
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'productcategory',
'field' => 'slug',
'terms' => explode(',',$terms),
),
),
);
$query = new WP_Query( $args );
Then just run a loop using the results of this query and you should be good to go!
I am developing a Wordpress plugin. I have some links for showing product Brands name and Product categories listing, in the url: http://testsite.com/product-categories and http://testsite.com/brands like that. So I want this link will be auto generated when plugin is activated and place in the default menu in Wordpress. How can I do that. I tried this code but its not working.
register_activation_hook( __FILE__, array( $this, 'myplugin_activate' ) );
public function myplugin_activate() {
$product_categories = array(
'post_content' => '',
'post_name' => 'product-categories',
'post_title' => 'Product Categories',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
$brands = array(
'post_content' => '',
'post_name' => 'brands',
'post_title' => 'Brands',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
wp_insert_post( $product_categories );
wp_insert_post( $brands );
}
Can anyone tell me how to do that? One thing I can not do that as page post type.
Searching for wp_insert_post and nav_menu_item, I found Programmatically add a Navigation menu and menu items where wp_create_nav_menu is suggested.
Should be a matter of using the functions wp_get_nav_menu_object, wp_create_nav_menu, wp_update_nav_menu_item and get_nav_menu_locations.
I am using a acf frontend form to create posts. All the fields are working but on frontend I want a field that should automatically asiign category at backend to the post.
I have custom post type name "person".
For adding the post from frontend form, here is the code
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_53c8f0941cec0'] ,
'post_category' => array(43,47) ,
'post_type' => 'person' ,
'submit_value' => 'Submit' ,
);
// insert the post
$post_id = wp_insert_post( $post );
.
The custom taxonomy name for my "person" custom post type is "person_type"
All the fields get saved but category does not gets saved at backend.
ANy help is really appreciated.
This should be pretty straight
Use
'tax_input' => array( 'CUSTOM_TAXONOMY_NAME' => array( COMMA SEPARTED VALUES OF TERMS)),
Example
$post = array(
'post_status' => 'Pending' ,
'post_title' => $new_title ,
'post_type' => 'products' ,
'tax_input' => array( 'products_type' => array( 11,33)),
'post_content' => $contentBlock,
'submit_value' => 'Submit' ,
);
$term = get_term_by( 'slug', 'your custom term slug', 'your custom taxonomy' );
$term_id = $term->term_id;
'tax_input' => array( 'your taxonomy' => $term_id )
this may help