0

I have merged two query post, but when I fetch their results, it shows duplicates.

Here is the code

$arg = array('post_type' => 'story','post_status' => 'publish', 'orderby' => 'date', 'order' => 'ASC','posts_per_page'  => -1,

                'meta_query' => array(

                    'relation' => 'AND',

                    array(

                        'key' => 'business_name',

                        'value' => $_POST['business_name'],

                        'compare' => 'LIKE'

                    )

                )

         $stories = new WP_Query($arg);

     $arg1 = array('post_type' => 'story','post_status' => 'publish', 'orderby' => 'date', 'order' => 'ASC','posts_per_page'    => 5);
                 $stories2 = new WP_Query($arg1);
                 $query = new WP_Query();
                 $query->posts = array_merge($stories->posts, $stories2->posts);

   foreach($query->posts as $post)
            { 
            <?php echo the_field('author_name'); ?>
      }

EDIT

From comments

there is a meta query in first arg so in first query post post only those post comes which has business_name meta has been set but it shows only 2 result so to fill space i add more post form that section so is there a way i can remove those post which come from arg ?

  • I cannot see your logic here. What are you trying to achieve – Pieter Goosen Nov 5 '14 at 7:25
  • Both queries are exactly the same except for posts_per_page – Pieter Goosen Nov 5 '14 at 7:26
  • sorry i pasted wrong args here i changed it. – Vishal Nov 5 '14 at 9:01
  • Your arguments are still exactly the same accept for posts_per_page. As I asked previously, what are you looking to do. As it stands, you will have 5 duplicate posts. If you remove those 5 duplicates, you are left with only the first query. So I ask again, what are you trying to achieve here as there are no logic in your code – Pieter Goosen Nov 5 '14 at 9:05
  • there is a meta query in first arg so in first query post post only those post comes which has business_name meta has been set but it shows only 2 result so to fill space i add more post form that section so is there a way i can remove those post which come from arg ? – Vishal Nov 5 '14 at 9:13
1

Before I start, you have a problem with your meta_query. You should not use relation for a single inner meta_query array

relation (string) - The logical relationship between each inner meta_query array when there is more than one. Possible values are 'AND', 'OR'. Do not use with a single inner meta_query array.

Your first query seems to be correct and I agree with that. The second query is basically there to fill posts if the first query have less than a certain amount of posts. There are a couple ways to achieve this

  • Making use of "placeholder posts" as described by @G.M. in this post

  • Making use of a second query to fill the rest of the posts

I'm not going to touch on @G.M. post or the implementation there of as he has already done it, so feel free to check out that post

On the second idea of using a second query to get posts, you will have to do a bit of planning.

Points to keep in mind here are:

  • Count the amount of posts in the first query. If it is less that a preset amount, calculate a number of posts that will be retrieved by the second query

  • Make use of a meta_query to retrieve posts in the second query that does not have the specified meta_value.

  • There is no need to merge your two queries, simply handle them separately, depending on what you want to do. But this is all up to you and what you are trying to achieve

Here is the concept:

(Caveat: All untested)

First thing you need to do is to get the number of posts returned by the first query. This can easily be done using $found_posts. In your case

$stories->found_posts

Once you have that, you can check that against a set value in a conditional statement. Say you need 10 posts to be shown

$min_posts = 10;
if( $stories->found_posts < $min_posts ) {
   //get posts from the second query
}

You now need to calculate the amount of posts you will need to get from the second query.

$ppp = $min_posts - $stories->found_posts;

$ppp will be the number of posts that will be retrieved and will thus be fed to posts_per_page

$arg1 = array(
    'post_type' => 'story',
    'post_status' => 'publish', 
    'orderby' => 'date', 
    'order' => 'ASC',
    'posts_per_page'  => $ppp,
    'meta_query' => array(
       array(
          'key' => 'business_name',
          'value' => $_POST['business_name'],
          'compare' => 'NOT LIKE'
       ),
    ),
);
$stories2 = new WP_Query($arg1);

You can now merge (if needed) and display your results

$posts = array_merge($stories->posts, $stories2->posts);

Final code:

(As stated, all untested)

$arg = array(
    'post_type' => 'story',
    'post_status' => 'publish', 
    'orderby' => 'date', 
    'order' => 'ASC',
    'posts_per_page'  => -1,
    'meta_query' => array(
       array(
          'key' => 'business_name',
          'value' => $_POST['business_name'],
          'compare' => 'LIKE'
       ),
    ),
);
$stories = new WP_Query($arg);

$min_posts = 10;
if( $stories->found_posts < $min_posts ) {
    $ppp = $min_posts - $stories->found_posts; 

    $arg1 = array(
        'post_type' => 'story',
        'post_status' => 'publish', 
        'orderby' => 'date', 
        'order' => 'ASC',
        'posts_per_page'  => $ppp,
        'meta_query' => array(
           array(
              'key' => 'business_name',
              'value' => $_POST['business_name'],
              'compare' => 'NOT LIKE'
           ),
        ),
    );
    $stories2 = new WP_Query($arg1);

    $posts = array_merge($stories->posts, $stories2->posts);
}else{
    $posts = $stories->posts;
}

foreach ( $posts as $post ) {
    echo $post->ID;
}
  • hi thanks peter for reply actully i tried array_unique with sort_regular option and its working fine with the object array instead of array_unique so should i keep that? – Vishal Nov 5 '14 at 12:04
  • It is all up to you. I have given you two methods which I think IMO works better and are more dynamic. That being said, I don't know if my solution actually works as I can't test it. You should probably use what you are comfortable with. If you think your solution is better or it works, post that as a proper answer and accept it. On the other hand, if mine answer worked, accept mine. The choices are up to you. :-) – Pieter Goosen Nov 5 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.