saidozcan saidozcan -3 years ago 1129
PHP Question

How to add new post without adding to database wordpress

I use a WordPress blog and I want to show a post without adding anything to database.
What I want to say is:

I generate a post when page loads,and prepend it in homepage.
I've searched and found

wp_insert_post()
function but it also add to database.
How can i do this with php?

For example:
There is a post array which is generated by a query.How can I insert my post to this array before page loaded?

I want clear my idea.Here's step by step what i want.

*1)*Im generating an array like that
$arr['title] = "my title",
$arr['content'] = "my content",

*2)*WP sends a query to database and have the posts am i right? And there is an array,to show on the theme and main page?
At this point i want to add my external array(generated in step1 ) to this array(generated by WP via a query)

3) By this way i will be able to add a post without adding it to my database.

Answer Source

You can simply add your virtual post in one of your theme templates as raw HTML.

Alternatively, if you're feeling adventurous, you could modify the main query results and include your post inside:

add_action('loop_start', function($query){

  // create the post and fill up the fields
  $post = new WP_Post((object)array(
    'ID'           => -1,
    'post_title'   => 'Bla blah',
    'post_content' => 'Your content',
  ));

  // add it to the internal cache, so WP doesn't fire a database query for it
  // -1 is the ID of your post
  if(!wp_cache_get(-1, 'posts'))
    wp_cache_set(-1, $post, 'posts');

  // prepend it to the query
  array_unshift($query->posts, $post);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download