在 WordPress 搜索查询结果中添加虚假帖子

问题描述 投票:0回答:1

我正在尝试在我的 WordPress 搜索结果中插入一些虚假帖子。使用 pre_get_posts 挂钩,我能够触发该函数,但我无法将假帖子添加到 WordPress 结果中。

我关注了另一篇关于插入假帖子的帖子。文章提到将假帖子插入 wp-cache 中。

任何帮助将不胜感激。

function extra_search_items($query) {
  if ($query->is_search && !is_admin()) {

    global $wp, $wp_query;

    $FakePosts = array(
            array(
              'ID'           => -199,
              'post_title'   => 'Fake 1',
              'post_content' => 'This is a fake virtual post.',
              'post_date'    => '2018-06-22 00:00:00',
              'comment_status' => 'closed',
              'post_type'     => 'post'
             ),
            array(
              'ID'           => -200,
              'post_title'   => 'Fake 2',
              'post_content' => 'This is a fake virtual post.',
              'post_date'    => '2018-06-22 00:00:00',
              'comment_status' => 'closed',
              'post_type'     => 'post'
             )

    );

    $i = 0;

    $post = array();

      foreach ($FakePosts as $blog) {

             // create the post and fill up the fields
             $post[$i] = new WP_Post((object)array(
              'ID'           => $blog['ID'],
              'post_title'   => $blog['post_title'],
              'post_content' => $blog['post_content'],
              'post_date'    => $blog['post_date'],
              'comment_status' => $blog['comment_status'],
              'post_type'     => $blog['post_type']
             ));

             if(!wp_cache_get($post[$i]->ID, 'posts')) {
              wp_cache_set($post[$i]->ID, $post[$i], 'posts');
              array_unshift($wp_query->posts, $post[$i]);
              $wp_query->post_count++;

             }
          $i++;
      }
  }

  return $wp_query;

}
add_action('pre_get_posts','extra_search_items'); 
php wordpress
1个回答
0
投票

您创建了 fakeposts 数组,但您没有将 fakeposts 与原始帖子合并。

合并两个数组并循环它,它也会显示假帖子,

就像下面这样

function extra_search_items($query) {
  if ($query->is_search && !is_admin()) {

    global $wp, $wp_query;

    $FakePosts1 = array(
            array(
              'ID'           => -199,
              'post_title'   => 'Fake 1',
              'post_content' => 'This is a fake virtual post.',
              'post_date'    => '2018-06-22 00:00:00',
              'comment_status' => 'closed',
              'post_type'     => 'post'
             ),
            array(
              'ID'           => -200,
              'post_title'   => 'Fake 2',
              'post_content' => 'This is a fake virtual post.',
              'post_date'    => '2018-06-22 00:00:00',
              'comment_status' => 'closed',
              'post_type'     => 'post'
             )

    );

    $i = 0;

    $post = array();
    $FakePosts_array = array_merge($FakePosts1,$FakePosts)
      foreach ($FakePosts_array as $blog) {

             // create the post and fill up the fields
             $post[$i] = new WP_Post((object)array(
              'ID'           => $blog['ID'],
              'post_title'   => $blog['post_title'],
              'post_content' => $blog['post_content'],
              'post_date'    => $blog['post_date'],
              'comment_status' => $blog['comment_status'],
              'post_type'     => $blog['post_type']
             ));

             if(!wp_cache_get($post[$i]->ID, 'posts')) {
              wp_cache_set($post[$i]->ID, $post[$i], 'posts');
              array_unshift($wp_query->posts, $post[$i]);
              $wp_query->post_count++;

             }
          $i++;
      }
  }

  return $wp_query;

}
add_action('pre_get_posts','extra_search_items'); 
© www.soinside.com 2019 - 2024. All rights reserved.