与假postmeta的Wordpress假岗位.

 吴姿云68153 发布于 2023-02-08 16:14

几天前我发了一篇文章" 如何为每个用户分别制作一个假的WordPress帖子 "

我设法通过使用此资源制作假帖子.除非我尝试使用自定义字段选项执行相同操作,否则一切都很好.我正在使用的主题有一个单独的自定义字段,wp_postmeta其中包含嵌入式视频

.
这是我尝试用于设置自定义字段选项的代码.

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 = '';

    $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'); 

我即兴创作的部分是$wp_query->meta_query=array($vid,$vid1);,但它没有帮助,因为它预期就好像即使设置这两个选项它也不会设置post_id,主题找不到哪个帖子是选项.伙计们有什么想法我怎么能这样做?

1 个回答
  • $wp_query->meta_query属性

    据我了解该$wp_query对象,您无法将结果数据直接添加到$wp_query->meta_query属性中以伪造自定义帖子元数据.

    它仅用于构造元查询,并且属于类型WP_Meta_Query.

    如果你看看源代码的WP_Query类,你会发现这些线路:

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

    这是WP_Meta_Query课程的描述.

    可能解决方案

    你可以尝试,而不是在get_post_metadata过滤器,或在一般的get_x_metadata地方x{post, comment, user}.

    这是一个例子:

    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 );
    

    使用时伪造video_url元键的值:

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

    在你的主题.

    类似于_oembed_576540b29025537e24e5bcdcae946a46元键.

    2023-02-08 16:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有