Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] 建立不存在 WP_POSTS 資料表中的「假文章/頁面」 https://www.mxp.tw/9104/
<?php
function create_fake_post($content, $title = 'Slider Revolution') {
$post = new stdClass();
$post->ID = -1;
$post->post_author = get_current_user_id();
$post->post_date = current_time('mysql');
$post->post_date_gmt = current_time('mysql', 1);
$post->post_title = $title;
$post->post_content = $content;
$post->post_status = 'publish';
$post->comment_status = 'closed';
$post->ping_status = 'closed';
$post->post_name = 'rs-fake-page-' . rand(1, 99999); //append random number to avoid clash
$post->post_type = 'page';
$post->filter = 'raw'; //important
//$post->post_meta = new stdClass();
//$post->post_meta->_wp_page_template= '../public/views/revslider-page-template.php';
//Convert to WP_Post object
$wp_post = new WP_Post($post);
//Add the fake post to the cache
wp_cache_add(-1, $wp_post, 'posts');
global $wp, $wp_query;
// Update the main query
$wp_query->queried_object_id = -1;
$wp_query->post = $wp_post;
$wp_query->posts = array($wp_post);
$wp_query->queried_object = $wp_post;
$wp_query->found_posts = 1;
$wp_query->post_count = 1;
$wp_query->max_num_pages = 1;
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_single = false;
$wp_query->is_attachment = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
$wp_query->is_tag = false;
$wp_query->is_tax = false;
$wp_query->is_author = false;
$wp_query->is_date = false;
$wp_query->is_year = false;
$wp_query->is_month = false;
$wp_query->is_day = false;
$wp_query->is_time = false;
$wp_query->is_search = false;
$wp_query->is_feed = false;
$wp_query->is_comment_feed = false;
$wp_query->is_trackback = false;
$wp_query->is_home = false;
$wp_query->is_embed = false;
$wp_query->is_404 = false;
$wp_query->is_paged = false;
$wp_query->is_admin = false;
$wp_query->is_preview = false;
$wp_query->is_robots = false;
$wp_query->is_posts_page = false;
$wp_query->is_post_type_archive = false;
//Update globals
$GLOBALS['wp_query'] = $wp_query;
$wp->register_globals();
return $wp_post;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment