Creating pages when activating plugin WordPress - wordpress

Anyone Please help!
I have a plugin which creates post_type pages in the backend. The plugin is creating the desired pages but the problem is whenever i try to see the page list, it shows "No pages found" message. Screenshot here: http://prnt.sc/azalub
My code for creating the required pages here:
$new_page = array('post_title' => $title,
'post_content' => '['.$shortcode.']',
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id = wp_insert_post( $new_page );

For this purpose, you need to register with plugin activation hook.
See the code example below:
function add_my_custom_page() {
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'My Custom Page' ),
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'add_my_custom_page');

While creating a custom post type, i had set 'query_var' to 'true' on one of the custom post type in my plugin. Setting it 'false' just made everything fine.

if you are using class try pass array($this, 'method_name') instead of function name.

Related

wp_insert_post Only Once for Custom Post Type

I have created a plugin that enables a Custom Post Type and I have inserted some default values for this Post Type with wp_insert_post.
wp_insert_post( array(
'ID' => '3',
'post_status' => 'publish',
'post_type' => 'exhibitor',
'post_title' => 'Title',
'post_content' => 'Description...'
) );
The problem is my values are reinserted everytime the page refreshes. I cannot edit or remove them.
How do I get Wordpress to only update a Custom Post Type when my plugin is activated? Everytime I refresh a page the posts are published again. So I cannot edit or delete the posts.
Regards,
In your plugin use register_activation_hook, that's plugin function to be run when the plugin is activated.
register_activation_hook(__FILE__, 'newplugin_install');
function newplugin_install() {
wp_insert_post( array(
'ID' => '3',
'post_status' => 'publish',
'post_type' => 'exhibitor',
'post_title' => 'Title',
'post_content' => 'Description...'
) );
}
hope this will work for you ;)

wordpress add page

i am writing plugin for wordpress and in this plugin admin should add some information like this ===> title,text in database.
I want that this information to be added as wordpress page in database.
What function should i use to make this task?
You can use wp_insert_post for that http://codex.wordpress.org/Function_Reference/wp_insert_post
Pages are just a type of posts in WP.
$post = array(
'post_title' => 'my Title',
'post_content' => 'my Text',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $post );

Adding pages/posts after plugin installation

There are a few plugins that I know that after installation they will create pages or posts. How do you do this?
Tried to research but could not find helpful guide.
The wp_insert_post reference on the WordPress Codex might help.
Here's an example code of how to create a new post programmatically:
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
To run this code automatically when your plugin is activated, you can wrap this code in the register_activation_hook hook.

How to Set POST permalink/slug in wordpress using wp_insert_post

I m writing simple script using wp_insert_post() to post article in blog.
but one problem here, I want to make Title and slug of an URL different.
How to achieve this?
for example:
Title: How to make your diet success
Slug: 7-ways-to-make-succes-Diet
post_title sets the title, and post_name sets the slug.
So:
// 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 );

Getting wp_list_categories() custom walker to paginate

I've got a custom post type registered, with custom taxonomies, everything good and clear.
I wish I could somehow display all the categories of the taxonomy with pagination.
I am using a custom category Walker and am thinking to register a custom page rewrite for page query, and add some code to the category walker to display only the desired interval. Am I on the right direction?
Also, wp_list_categories sends to the category Walker the entire list of categories. Is there any way to get only the desired interval?
In this particular setup, no.
But I found a workaround: I registered a function that created a custom-type post each time I added a category in the taxonomy. That way I used the archive feature for custom post types, made available in 3.1-RC1.
function create_crew_post_on_term($term_id) {
$term = get_term($term_id, 'crew');
$post = array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_author' => 1,
'post_content' => '',
'post_date' => date('Y-m-d H:i:s'),
'post_excerpt' => '',
'post_name' => $term->slug,
'post_status' => 'publish',
'post_title' => $term->name,
'post_type' => 'crew'
);
wp_insert_post( $post );
}
add_action('created_crew', 'create_crew_post_on_term');

Resources