Hoping you can help me with my problem.
I am trying to pull posts that have match the same word from another custom post type to the standard WP posts.
For example: A news post about "BMW M5 being announced with new safety features" and then a product page about a BMW M5.
On the news post I want to have a click out to the product page and pull through some basic information from the CPT and want to link the two by their name i.e post title of the CPT. As the "news" posts will have more than just "BMW M5" I'd need to do a kind of reversal wordpress query where it looks for all news posts which match the car product custom post type but on the news page.
I tried using a piece of code which I found on here that searches for similar posts using a modified query in the loop args but because I think it is meant to work for more words than the product pages offer it returns nothing.
For a stop gap I am looking for an exact tag on both but it is not ideal as it is open to error on the human side and would like to automate it as much as possible.
Here is the current code using the tags.
$tags = wp_get_post_tags($post->ID);
$tag_ids = array();
foreach ($tags as $individual_tag) $tag_ids[] = $individual_tag->slug;
$args = array(
'posts_per_page' => 1,
'post__not_in' => array($post->ID),
'post_type' => 'games',
'post_status' => array('publish', 'draft'),
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag_ids
)
)
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()){
while ( $wp_query->have_posts() ) { $wp_query->the_post(); ?>
<?php $src = wp_get_attachment_image_src( get_field('image'), $size, false ); ?>
<?php
$originalDate = get_field('release_date');
$newDate = date("d/m/Y", strtotime($originalDate));
?>
<?php if(get_post_status() == 'publish'){ ?>
<div class="hub" style="background-image: url('<?php echo $src[0]; ?>')">
<dl id="gdbinfobar">
<dt class="info_bar_title">
<h3 style="color: #FFF"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
</dt>
<dt class="info_bar_release">
Released on: <?php echo $newDate; ?>
</dt>
<dt class="info_bar_model">
<?php echo get_field('t_platform'); ?>
</dt>
</dl>
<a class="btn" href="<?php the_permalink()?>">View Game</a>
</div>