gpt4 book ai didi

php - 带有假 postmeta 的 Wordpress 假帖子。

转载 作者:IT老高 更新时间:2023-10-29 00:17:16 29 4
gpt4 key购买 nike

几天前我发了一个帖子“How to make a fake WordPress post for each user separately

我设法通过使用 this 发布了假帖子资源。除非我尝试对自定义字段选项做同样的事情,否则一切都很好。我使用的主题在 wp_postmeta 中有一个单独的自定义字段其中包括单独的 <div> 中的嵌入视频.
这是我尝试用于设置自定义字段选项的代码。

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

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

最佳答案

$wp_query->meta_query 属性

据我了解 $wp_query 对象,您不能将结果数据直接添加到 $wp_query->meta_query 属性来伪造自定义帖子元数据。

它仅用于构建元查询,类型为 WP_Meta_Query

如果您查看 source code对于 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 元键。

关于php - 带有假 postmeta 的 Wordpress 假帖子。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733218/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com