Wordpress - multiple WP Query objects into one?

前端 未结 2 1439
Happy的楠姐
Happy的楠姐 2021-01-14 05:22

In Wordpress it\'s possible to create own WP Querys for the loop. An example is this:

$my_query = new WP_Query(array(\'post_parent\' => 3, \'post_type\' =         


        
2条回答
  •  轮回少年
    2021-01-14 05:40

    In case you don't want to do it with SQL, this is how I did my search page.

    Basic problem: When doing a meta_query, wordpress thinks I want the condition to be joined with "AND" instead of "OR".

    So Wordpress looks for a page with title/content = "myContent" AND the aioseop_keyword "myContent". This (in my case) lead to zero results, despite there was a page with matching SEO keyword.

    To get around this, I make two queries. Sounds simple, BUT: the Loop didn't want to recognize the posts, despite there are posts in the $post object. I found this solution after taking a look on the have_posts() function : it refers to other variables than just the $post object.

    $term = get_search_query(); // same as $_GET['s']
    
    # the normal search:
    $wordpress_keyword_search =& new WP_Query(array(
      's'         => $term,
      'showposts' => -1
    ));
    
    # now push already found post IDs to an array, so we can exclude them from the meta search.
    foreach ($wordpress_keyword_search->posts as $post_)
      $exclusion[] = $post_->ID;
    
    
    # now do the meta query search
    $aioseop_keyword_search =& new WP_Query(array(
      'post__not_in' => $exclusion,
      'post_type' => 'any',
      'showposts' => -1,
      'meta_query' => array(            
        array(
          'key'       => '_aioseop_keywords',
          'value'     => $term,
          'compare'   => 'LIKE',
        )
      )
    ));
    
    # merge the two array posts.
    # post_count and found_posts must be added together also. 
    # otherwise have_posts() returns false.
    # see: http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/query.php#L2886
    
    $wordpress_keyword_search->posts       = array_merge($wordpress_keyword_search->posts, $aioseop_keyword_search->posts );
    $wordpress_keyword_search->found_posts = $wordpress_keyword_search->found_posts + $aioseop_keyword_search->found_posts;
    $wordpress_keyword_search->post_count  = $wordpress_keyword_search->post_count + $aioseop_keyword_search->post_count;
    

    Then use this in a simple loop:

    if ($wordpress_keyword_search->have_posts()) {
      while($wordpress_keyword_search->have_posts()) {
        $wordpress_keyword_search->the_post();
        # now you simply can:
        the_title();
        the_content();
    
      }
    } else {
      echo '

    Sorry, no posts found

    '; }

提交回复
热议问题