WordPress fake post with fake postmeta.

Several days ago I have made a post “How to make a fake WordPress post for each user separately

I managed to make a Fake post by using this resource. Everything was good unless I tried to do the same with custom field option. The theme I’m using has a separate custom field in wp_postmeta which includes the embedded video in separate <div>.
Here is the code I tried to use for setting the custom field option.

Read More
function kpg_f_content() {
    global $wp_query;

    $post = new stdClass();
    $post -> ID = 1;
    $post -> post_category = array('uncategorized');
    //Add some categories. an array()???
    $post -> post_content = 'hey here we are a real post';
    //The full text of the post.
    $post -> post_excerpt = 'hey here we are a real post';
    //For all your post excerpt needs.
    $post -> post_status = 'publish';
    //Set the status of the new post.
    $post -> post_title = 'Fake Title 1';
    //The title of your post.
    $post -> post_type = 'post';
    //Sometimes you might want to post a page.
    $post -> post_date = '[ 2013-12-19 5:34:3 ]';
    //The time post was made.
    $post -> post_date_gmt = '[ 2013-12-19 5:34:3 ]';
    //The time post was made, in GMT.

    $vid = new stdClass();
    $vid -> meta_key = 'video_url';
    $vid -> meta_value = 'http://www.youtube.com/watch?v=ucivXRBrP_0';

    $vid1 = new stdClass();
    $vid1 -> meta_key = '_oembed_576540b29025537e24e5bcdcae946a46';
    $vid1 -> meta_value = '<iframe width="500" height="281" src="http://www.youtube.com/embed/ucivXRBrP_0?feature=oembed" frameborder="0" allowfullscreen></iframe>';

    $wp_query -> queried_object = $post;
    $wp_query -> post = $post;
    $wp_query -> found_posts = 2;
    $wp_query -> post_count = 2;
    $wp_query -> max_num_pages = 2;
    $wp_query -> is_single = 1;
    $wp_query -> is_404 = false;
    $wp_query -> is_posts_page = 0;
    $wp_query -> posts = $post;
    $wp_query -> page = false;
    $wp_query -> is_post = true;
    $wp_query -> page = false;
    $wp_query -> meta_query = array($vid, $vid1);

}

add_action('wp', 'kpg_f_content'); 

The part I improvised is $wp_query->meta_query=array($vid,$vid1);, but it doesn’t help as it expected to be as if even it would set these 2 options it would not set the post_id and the theme could not find for which post was the option made.
Guys any ideas how can I perform this?

Related posts

Leave a Reply

2 comments

  1. The $wp_query->meta_query attribute

    As far as I understand the $wp_query object, you can’t add the result data directly to the $wp_query->meta_query attribute to fake the custom post meta data.

    It’s only for constructing the meta queries and is of the type WP_Meta_Query.

    If you check out the source code for the WP_Query class, you will find these lines:

    // Parse meta query
    $this->meta_query = new WP_Meta_Query();
    $this->meta_query->parse_query_vars( $q );
    

    Here’s a description of the WP_Meta_Query class.

    Possible Solution

    You can instead try the get_post_metadata filter, or in general get_x_metadata where x is in {post, comment, user}.

    Here’s an example:

    function custom_get_post_metadata( $meta_value, $object_id, $meta_key, $single ) 
    {
        // Change the meta value of the meta key 'video_url' 
        // for post id 1 when 'single' is TRUE
        if(   1 === $object_id 
           && TRUE === $single 
           && 'video_url' === $meta_key )
        {
            $meta_value = 'http://www.youtube.com/watch?v=ucivXRBrP_0';
        }
        return $meta_value;
    }
    add_filter( 'get_post_metadata', 'custom_get_post_metadata', 99, 4 );
    

    to fake the value of the video_url meta key when you use:

    echo get_post_meta( get_the_ID(), 'video_url' , TRUE );
    

    in your theme.

    Similar for the _oembed_576540b29025537e24e5bcdcae946a46 meta key.

  2. For all I know, the meta_query is actually an array, not an object so, try:

    $vid = array('meta_key' => 'video_url', 'meta_value' => 'http://www.youtube.com/watch?v=ucivXRBrP_0');
    $vid1 = array('meta_key' = '_oembed_576540b29025537e24e5bcdcae946a46', 'meta_value' => '<iframe width="500" height="281" src="http://www.youtube.com/embed/ucivXRBrP_0?feature=oembed" frameborder="0" allowfullscreen></iframe>');