I am trying to add a post to a category under taxonomy cate. The code I am using is:
$user = get_user_by( 'email', $_POST['user'] );
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'taxonomy' => ('cate'),
'post_type' => 'ad',
'post_category' => array(425),
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
The post is added but it's added under category 'uncategorized" and not under desired category ID. This system works properly when custom post type is not used. (In this case taxonomy 'cate')
Any ideas?
You need wp_set_object_terms, which takes post ID, terms, taxonomy, and append as parameters. For example:
$user_id = wp_insert_post( $id );
wp_set_object_terms( $user_id, 'cate', 'category', true );
I solved it like this:
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'post_type' => 'ad',
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
wp_set_object_terms($user_id, 416, 'cate', true);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
Josh showed me the way, but his syntax wasn't right. It' category first, taxonomy second, and some things had to be removed.
Related
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'post_status' => 'private',
'post_type' => 'student'
);
wp_insert_post($student);
This is how I'm inserting post from the frontend. it is working fine.
But I need to save some custom fields also from frontend along with post and need show the values in backend(just in text format not in input field). So is there anything exists like this 'my_custom_filed' => 'custom_value':
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'my_custom_filed' => 'custom_value'
'post_status' => 'private',
'post_type' => 'student'
);
wp_insert_post($student);
So I can print the value of custom fields in backend
function studentDataMetaBoxStructure(){
global $post;
$my_custom_filed = get_post_meta($post->ID,'my_custom_filed',true);
echo $my_custom_filed;
}
But I'm getting nothing in $my_custom_filed;
This is how you save meta box while post insertion.
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'my_custom_filed' => 'custom_value'
'post_status' => 'private',
'post_type' => 'student'
);
$the_post_id = wp_insert_post($student);
update_post_meta( $the_post_id, 'custom_meta_box_key', 'custom_meta_box_value' );
I am trying to access user meta by $wpdb query after a user registers.
What I am trying to do is to make 2 different custom post types if a user is a freelancer or an employer.
here is the code:
add_action('user_register','registration_funtion', 999);
function registration_funtion($uid){
global $wpdb;
$existsD = $wpdb->get_var( $wpdb->prepare(
"SELECT meta_value FROM wp_usermeta WHERE user_id = '%s' AND meta_key = 'user_reg_type'" , $uid
));
if($existsD == "userfreelancer"){
$my_post_2 = array(
'post_title' => sanitize_text_field($user_info->user_login),
'post_status' => 'publish',
'post_author' => $uid,
'post_type' => 'freelancer',
'post_content' => $existsD.'-'.$uid
);
$freelancer_id = wp_insert_post($my_post_2);
} elseif($existsD == "useremployer") {
$my_post = array(
'post_title' => sanitize_text_field($user_info->user_login),
'post_status' => 'publish',
'post_author' => $uid,
'post_type' => 'employer',
'post_content' => $existsD.'-'.$uid
);
$company_id = wp_insert_post($my_post);
}
Problem is its just takes the first if statement and completely ignores the else if statement.
Thanks in advance.
Regards,
Try this
add_action('user_register','registration_funtion', 999);
function registration_funtion($uid){
global $wpdb;
$existsD = get_user_meta($uid, 'user_reg_type');
if($existsD == "userfreelancer"){
$my_post_2 = array(
'post_title' => sanitize_text_field($user_info->user_login),
'post_status' => 'publish',
'post_author' => $uid,
'post_type' => 'freelancer',
'post_content' => $existsD.'-'.$uid
);
$freelancer_id = wp_insert_post($my_post_2);
}elseif($existsD == "useremployer") {
$my_post = array(
'post_title' => sanitize_text_field($user_info->user_login),
'post_status' => 'publish',
'post_author' => $uid,
'post_type' => 'employer',
'post_content' => $existsD.'-'.$uid
);
$company_id = wp_insert_post($my_post);
}
I registered a custom taxonomy with this code:
register_taxonomy( 'user_r_category', array( 'user_r' ), $args );
Now I try to insert a post to 'user_r_category' taxonomy within the category ID 7 and the post_type 'user_r':
$new_post = array(
//'ID' => '',
'post_author' => $current_user->ID,
//'post_category' => array(7),
'post_type' => 'user_r',
'post_content' => $r_textarea,
'post_title' => $r_title,
'tax_input' => array(
'user_r_category' => array( 7 )
),
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
The post was created, but not with the category 7. How could this work?
You would need to use:
1- get_term_by to get term obj
2- wp_set_object_terms to set custom taxonomy's term with post
$post_id = wp_insert_post($new_post);
$taxonomy = 'user_r_category';
$termObj = get_term_by( 'id', 7, $taxonomy);
wp_set_object_terms($post_id, $termObj, $taxonomy);
You can do it directly when inserting your post, with tax_input parameter :
$post = array(
'post_title' => $my_sanitized_title,
'post_content' => $my_sanitized_text,
'post_status' => 'publish',
'post_type' => 'post',
'tax_input' => array(
'hierarchical_tax' => array($my_hierarchical_tax_id)
),
);
$post_id = wp_insert_post($post);
This is an array. See the doc for detailed parameters : https://developer.wordpress.org/reference/functions/wp_insert_post/
i developed an WordPress plugin,it is a complex plugin,a nd now what remained to do i want to create a Page Parent and Child Pages that belongs to Page Parent.
My code for page creation is below, first function will create a parent page, and second function is also creating a parent page, i can not make a child of the first page, there is post_parent but how to get the id of that page parent that i create first?
register_activation_hook( __FILE__, 'create_page_1_parent');
function create_page_1_parent()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => '1first post',
'post_status' => 'publish' ,
'post_title' => 'parent',
'post_type' => 'page',
'post_parent' => '',
'post_content' => '[il_login_form]'
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue );
}
register_activation_hook( __FILE__, 'create_page_1_parent_child');
function create_page_1_parent_child()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => '1first post',
'post_status' => 'publish' ,
'post_title' => 'parent',
'post_type' => 'page',
'post_parent' => '', //what i have to put here that will go udner parent page
'post_content' => '[il_login_form]'
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
Just do both of the inserts in 1 function.
function create_page_1_parent()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => '1first post',
'post_status' => 'publish' ,
'post_title' => 'parent',
'post_type' => 'page',
'post_parent' => '',
'post_content' => '[il_login_form]'
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue );
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => '1first post',
'post_status' => 'publish' ,
'post_title' => 'parent',
'post_type' => 'page',
'post_parent' => $newvalue, //what i have to put here that will go udner parent page
'post_content' => '[il_login_form]'
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
}
Im trying to insert the category
$apples = "granny smith"
as part of a new post.
I can create a new post with a TAG easily with the following script:
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'tags_input' => $apples,
'post_type' => 'post'
);
wp_insert_post( $my_post );
But for some reason, despite that the WP codex lists
'post_category'
for categories, the following code does NOT create the new category "granny smith" and instead, enters the new post as "uncategorized" :
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => $apples
'post_type' => 'post'
);
wp_insert_post( $my_post );
Can someone help me w the code please? Where am I going wrong?
Accordning to the documentation it looks like the categories should be added as an array with the category id's:
$id_for_category_ganny_smith = 12;
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($id_for_category_ganny_smith),
'post_type' => 'post'
);
wp_insert_post( $my_post );
The documentation states:
Categories need to be passed as an array of integers that match the
category IDs in the database. This is the case even where only one
category is assigned to the post.
If you don't know the id of the categories you want to add, you can try the following:
$apples_slug = "granny_smith"
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
);
$post_ID = wp_insert_post( $my_post );
wp_set_post_terms( $post_ID, $apples_slug, 'category')