I'd like to get it from two queries and remove duplicate post IDs.

Asked 2 years ago, Updated 2 years ago, 491 views

What do you want to do
There are two.
1. Posts retrieved from two queries have duplicate posts, so I would like to exclude them
2. I want to set up to 3 display items

Current State
I'm creating a hotel introduction site.Therefore, the hotel's individual article page shows the hotels in the individual article and other related hotels.
The method is to retrieve posts with the same category as your current post from two queries, "dest_arg" and "travel theme ($reise_arg)" respectively, and combine the two queries to display the posts, but there are also duplicate posts.

The reason I thought about it was that the same post was retrieved from "$dest_arg" and "$rise_arg" and put them together as $new_query, so I think the same post was displayed unfiltered.

example
In the case of the hotel A article page, I would like to display only Hotel B and Hotel D as related hotels, but
"$dest_arg" =>Retrieved by Hotel B & Hotel D
"$reise_arg" =>Retrieved by Hotel B & Hotel D
"$new_query" => Display Hotel B and Hotel D 2 at a time
This is the current situation.

Category hierarchy
Category=>Hotels
Category=>Events

Parent Category 1=>Destination (ID=15)
Child Category 1=>Japan
Child Category 1 Child Category=>Tokyo, Osaka, Okinawa

Child Category 2=>UK
Child Category 2 Child Category=>Liverpool, London

Parent Category 2=>Travel Theme (ID=2)
Child category=> Shopping, eating and walking, visiting museums

Article Category Distribution
Hotel A=>Hotel, Destination, Japan, Tokyo, Travel Theme, Shopping
Hotel B=>Hotel, Destination, Japan, Osaka, Travel Theme, Eating & Walking, Shopping
Hotel C=>Hotel, destination, UK, London, travel theme, museum tour
Hotel D=>Hotel, Destination, Japan, Tokyo, Travel Theme, Shopping

There is also an event (ID=13) page, so there is an article like the one below. (Set to 'category_name' = > 'hotels' to get only hotels)
Event A=> Event), Destination, Tokyo, Travel Theme, Shopping
Event B=>Event, destination, Osaka, travel theme, eating and walking
Event C=> Event, destination, Okinawa, travel theme, museum tour

//single-hotel.php

// Obtain the ID of the destination child category of the current post
foreach((get_the_category())as$childcat){
  if(cat_is_instor_of(15,$childcat)){
    $dest_ID = $childcat->cat_ID;
  }};

// Obtain the child category ID of the travel theme for the current post   
foreach((get_the_category())as$childcat){
   if(cat_is_instor_of(2,$childcat)){
       $reise_ID = $childcat->cat_ID;
    }};

// Get Destination Articles
$dest_arg = array(
    'post__not_in' = > array($post->ID),
    'category_name' = > 'hotels',
    'category__in' = > $dest_ID,
    'category__not_in' = > array(13,14),
    'orderby' = > 'land',
    // 'posts_per_page' = > 3,
);

// Get travel theme articles
$reise_arg = array(
    'post__not_in' = > array($post->ID),
    'category_name' = > 'hotels',
    'category__in' = > $reise_ID,
    'category__not_in' = > array(13,14),
    'orderby' = > 'land',
    // 'posts_per_page' = > 3,
);

$destination_query=newWP_Query($dest_arg);
$reise_query=newWP_Query($reise_arg);

$new_query=newWP_Query();
$new_query->posts=array_merge($destination_query->posts,$rise_query->posts);
// $new_query=array_unique($dest_arg,$rise_arg);
$new_query->post_count=$destination_query->post_count+$rise_query->post_count;

if($new_query->have_posts():
   while($new_query->have_posts():
   $new_query->the_post();
?>

"I thought of the logic of ""storing the ID of the post obtained when the post was retrieved in an array to delete the duplicate ID"", but I don't know how to write the code, so I would appreciate it if you could advise me."

Also, I would like to display up to three related hotels, but in this case, is there a limit like 'posts_per_page' in '$new_query=new WP_Query();'?Currently, the number of posts retrieved, such as 6 is reflected.

Thank you for your cooperation.

Tried
I wanted to limit the number of displays with the code below, but nothing changed.
$new_query=newWP_Query('posts_per_page=3');

Here's the link I used when I merged the two queries: Reference link

***Resolved***
By excluding duplicate post IDs in array_unique, I was able to get three random IDs in the array with the code below, and I was able to display them.
array_intersect_key($unique_arr, array_flip(array_land($unique_arr,3));
Reference Links

 'fields' = > 'ids' // Add this code to $reise_arg and $dest_arg

$all_IDs=array_merge($destination_query->posts,$rise_query->posts); 
$unique_arr =array_unique($all_IDs);
$get_three_IDs = array_intersect_key($unique_arr, array_flip(array_land($unique_arr,3));

$new_query=newWP_Query(array(
     'post_in' =>$get_three_IDs,));

php wordpress

2022-09-30 22:00

1 Answers

post__not_in
You are using it, but how about using it as it is?

Flow processing is

(1) Obtain relevant destination articles

(2) Array the IDs of the articles

(3) Get travel theme articles
At that time, post_not_in excludes articles obtained in related destination articles in addition to the conditions.

I don't think it's that difficult.

The limitation of articles is that I have acquired articles twice in two categories, so it's quite troublesome, but
You can adjust the number of posts_per_page numbers you want to retrieve for the second time using variables.

(1) Obtain 3 articles related to the purpose first

(2) The actual number of cases obtained is N (up to 3 minimum 0)
If 3-N = 0, don't get travel theme article
In case of 3-N>0, get (3-N) travel theme articles


2022-09-30 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.