I am using the Wordpress plugin WP User Frontend. I have a POST form setup that creates a post in a custom post type. I want that same post when setup to create another post in another custom post type. For this secondary post, I want to use the post_title as it's title.
I have:
add_action('create_whiteboard_hook', 'create_whiteboard', 10, 3 );
function create_whiteboard( $form_id, $post_id, $form_settings ) {
$post_id = wp_insert_post(array (
'post_type' => 'whiteboard',
'post_title' => $_POST['post_title'],
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
));
}
This works OK (as in it does create an additional post on the whiteboard (custom post type)) but it comes through with a title of (no title). I can manually set the title but I want it to automatically get it from the title the user puts in...
Related
Further i need the automatically created page to be edited by the respective subscribe only and when published it should be visible to all users who visit the website
Perhaps you need to use the user_register hook and execute the function in which you create the post, and to create a post, you need to use wp_insert_post function
add_action('user_register','my_function');
function my_function($user_id){
// Create post object
$my_post = array(
'post_title' => 'Title of page',
'post_type' => 'post', // Set type of your post
'post_content' => $_POST['post_content'],
'post_status' => 'publish', // Page published
'post_author' => $user_id // Assign page author
);
// Insert the post into the database
wp_insert_post( $my_post );
}
You can also manage Roles and Capabilities for your post type
Examples:
https://wordpress.stackexchange.com/a/208739/193674
https://wordpress.stackexchange.com/a/108375/193674
I am developing a blog site with wordpress. I want users to log in in my site , user should be able to post on the website , but post will be published only when admin approves of it . Please help
After login, user add post from admin or front end post form?
if user post from front end then use below code:
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'pending', // use draft also
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
after this code, admin can change status from pending to publish.
Use this plugin.
WP User frontend
Hi guys i am trying to add post in WordPress. I used CPT UI to created/register custom post type and custom taxonomies. I used the code below to add my post.
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => 'questions',
);
$post_id = wp_insert_post($post);
$check_if_save = wp_set_object_terms( $post_id , array(10,11 ), 'question-category' );
But when this code executed $check_if_save->get_error_message() always returns an Invalid Taxonomy. I also check the get_taxonomies() but my custom taxonomy is not included. I also register taxonomy manually and follow this link as a solution. But i doesn't work for me.
Any ideas why this happen?
Make sure you're creating your taxonomies before the custom post type they belong to.
I have been trying this project of mine for a while now and I have got no luck. I'm trying to create 8 post programmatically in functions.php. I need these to only post 1 time. The problem I've been having is everytime I refresh the page the post will just auto create more. Here is my code to create post progammatically in functions.php.
<?php // Create post object
$my_post = array(
'post_title' => 'How to make your diet success',
'post_name' => '7-ways-to-make-succes-Diet',
'post_content' => 'my content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?>
The only proble with this code is that it auto creates more post every time the page refreshes. I'm going to be creating 8 of these functions and I only want them to be created once. A code example would be great.
Next, I want to display the post on my index.php. I want to get these post individually. Here is my code I have so far.
<div class="post1"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
<div class="post2"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
I'm pretty sure I need to either call the slugs or post name to get them individually. Yes, I have tried this method as well as 10 other methods but nothing has worked. The closest I got was it to display the post name. Code examples would be great. I will be so grateful and probably donate some money via paypal if someone can get this working for me. Thanks.
functions.php is not a good place for programatically create pages nor posts. You should create a plugin (it is simple as creating custom theme) and create posts in its activation function. This function is called only on your plugin activation. Read also about plugin deactivation a uninstall hooks
The reason why your posts get created again and again is that file functions.php is called each time the page is requested. If you insist on creating posts in functions.php, you should wrap your wp_insert_post by a condition chcecking whether your posts are already created - than get_posts function would suit your needs.
<?php
//Use either post slug (post_name)
$post = get_posts( array( 'name' => '7-ways-to-make-success-diet' ) );
/*or $post = get_posts( array( 'name' => sanitize_title('My Single.php Test') ) );
if you do not set the post_name attribute and let WordPress to set it up for you */
if ( empty($post) ) {
// Create post object
$my_post = array( 'post_title' => 'My Single.php Test', 'post_name' => '7-ways-to-make-success-diet', 'post_content' => 'my content4654654', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) );
// Insert the post into the database
wp_insert_post( $my_post );
}
?>
Also, get_posts will help you to bring your posts on front page. Eg.
<?php
$post = get_posts( array( 'name' => 'How to make your diet success' ) );
echo $post->post_title;
...
?>
I have a WordPress page created dynamically
$my_post = array(
'post_title' => 'page-for-download',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
now my question is how to assign page template to this page dynamically
WordPress keeps the page template in a post meta entry(named _wp_page_template). Here is what you should do once you create the page:
update_post_meta( $new_post_id, '_wp_page_template', 'custom-template.php' );
Where $new_post_id is the result of wp_insert_post()(I assume that this is what you are using to create the new post). Note, that you might want to check to see if you have an actual id(by default wp_insert_post() will return false if it fails to create a new post).
You can see that information in the first NOTE in the Parameters section of the WordPress codex page Function Reference/wp insert post