Save custom meta box too while inserting post from frontend wordpress - php

$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' );

Related

wp_insert_post with custom taxonomy

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/

Woocommerce filter query backend

How do I filter the product to specific things?
I got the next code:
$post_query .= array(
'relation' => 'AND',
array(
'post_type' => 'tc_events',
'post_author' => $user_ID
),
array(
'post_type' => 'tc_templates',
'post_author' => $user_ID
),
array(
'post_type' => 'tc_templates',
'post_title' => 'Default'
),
);
$query->set( $post_query );
I would like to filter out some post_types to specific author and a post_type to a post title.
I guess I got something wrong, I don't know how edit the post query...

Wordpress, add post to custom taxonomy's category

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.

ACF frontend form wordpress category not working

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

Inserting tags v categories via PHP

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')

Categories