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
}
Related
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 have made an action that allows a page to be made if the switch in the setting is checked. After it is confirmed it's checked it does create the page, but without the template added. How do I get my template to work?
add_action( 'admin_init', 'cart_page' );
function cart_page() {
//Add cart page to website
if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
// IF CART HAS BEEN CHECKED
$new_page_id = wp_insert_post( array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
// Assign page template
'page_template' => plugins_url('page_templates/cart_template.php', __FILE__ )
) );
wp_insert_post($new_page_id);
update_option( 'cart_exist', true );
}
else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
// IF CUSTOMER DOES NOT WANT CART
update_option( 'cart_exist', false );
}
}
This is what my template page looks like in the plugin.
get_header();
echo do_shortcode( '[cart_portal]' );
get_footer();
?>
In my case, I have figured out a solution for the simple problem. If I just need to add a shortcode I can add it to 'post_content'. I would still like to know how to add template. But you can use a file to import the entire layout if you'd like still. But it cannot be used with visual composers.
See example below...
$new_page_id = array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[cart_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
);
wp_insert_post($new_page_id);
I have a button in the frontend and once clicked on it both logged-in and not logged in users left a message and I'm using wp_insert_post() function for adding new message, here is my code:
$post_id = wp_insert_post(array (
'post_type' => 'messages',
'post_title' => $title,
'post_content' => $message,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'comment_status' => 'closed',
'post_author' => 1,
'ping_status' => 'closed',
'tax_input' => array(
'message-category' => array(26),
)
));
But except adding new post, I need to set taxonomy category as well. But category is being set only when user is logged in. For not logged in users it is not working. I tried to add post_author (check above), but again that doesn't help.
Also tried to use below function, but again no luck and category is not being set when not logged in users left a message:
wp_set_object_terms( $post_id, array(26), 'message-category', true );
And also tried with post_category but again no luck.
Any ideas please?
You can use post_category as an argument of wp_insert_post function. Please refer to doc.
You function should look like this:
`
$post_id = wp_insert_post(array (
'post_type' => 'messages',
'post_title' => $title,
'post_content' => $message,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'comment_status' => 'closed',
'post_author' => 1,
'ping_status' => 'closed',
'post_category' => array(6)
)
);
`
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.
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')