Wordpress multiple WP request objects in one?

In Wordpress, you can create your own WP Query for the loop. Example:

$my_query = new WP_Query(array('post_parent' => 3, 'post_type' => 'page'));

Another example is the following:

$my_query = new WP_Query(array('cat' => 1, 'post_type' => 'post'));

I need a loop that represents pages AND posts from the same loop.

Now to my question. Is it possible to combine these two objects into one? If so, how? I am NOT interested in creating two different loops.

+3
source share
3 answers

I found a solution myself. I use setup_postdata to parse an SQL query.

Decision

+2
source

what you want is translated into a condition WHERE ... OR ...or UNIONin SQL, for example.

SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page') 
  OR (cat = 1 AND post_type = 'post')

or

SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
  UNION
SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'

WP SQL WP_Query(), , : OR'ing UNION .

, , - , posts_where ( WHERE , ). WP, WHERE OR .

. http://codex.wordpress.org/Custom_Queries

+2

SQL, .

: meta_query wordpress , , "AND" "OR".

, Wordpress /content = "myContent" aioseop_keyword "myContent". ( ) , SEO.

To get around this, I make two queries. It sounds simple, BUT: Loop did not want to recognize messages, despite the fact that there are entries in the $ post object. I found this solution by looking at the has_posts () function : it refers to other variables, not 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 '<p>Sorry, no posts found</p>';
}
+1
source

Source: https://habr.com/ru/post/1716296/


All Articles