After hitting a dead end with relationship fields, have decided to go the route of making a hidden taxonomy that links with a custom post type. The custom post type called Game is linked to the hidden _game taxonomy. I have then added posts to the taxonomy to ensure everything is connected. I am now trying to display the results. I have made a template for the game, where all the information about the game is listed. It also acts as an archive for all related posts within the connected taxonomy. The only issue is it is not working as intended. I have it setup so it is using taxonomy query with the it set up as follows:
Taxonomy: Game
Field: Slug
Terms: {{get_the_ID}}
Compare: IN
The code to link the CPT and Tax is as follows:
<?php
/**
* When a game CPT entry is saved, automatically create an entry in our pseudo-hidden "_game" taxonomy.
*
* @uses get_post_type()
* @uses get_post()
* @uses get_term_by()
* @uses wp_insert_term()
*
* @const DOING_AUTOSAVE
*
* @param int $post_id
*/
function update_game_shadow_taxonomy( $post_id ) {
// If we're running an auto-save, don't create a term
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// If we're not working with a game, don't create a term
if ( 'game' !== get_post_type( $post_id ) ) {
return;
}
// If we can't retrieve the game, don't create a term
$game = get_post( $post_id );
if ( null === $game || 'auto-draft' === $game->post_status ) {
return;
}
// If the game already exists, don't create a term.
$term = get_term_by( 'name', $game->post_title, '_game' );
if ( false === $term ) {
// Create the term
wp_insert_term( $game->post_title, '_game' );
}
}
add_action( 'save_post', 'update_game_shadow_taxonomy' );
/**
* Remove a term from the shadow taxonomy upon post delete.
*
* @uses get_post_type()
* @uses get_post()
* @uses get_term_by()
* @uses wp_delete_term()
*
* @const DOING_AUTOSAVE
*
* @param int $post_id
*/
function delete_game_shadow_tax_term( $post_id ) {
// If we're running an auto-save, don't delete a term
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// If we're not working with a game, don't delete a term
if ( 'game' !== get_post_type( $post_id ) ) {
return;
}
// If we can't retrieve the game, don't delete a term
$game = get_post( $post_id );
if ( null === $game ) {
return;
}
// If the game already exists, don't delete anything.
$term = get_term_by( 'name', $game->post_title, '_game' );
if ( false !== $term ) {
// Delete the term
wp_delete_term( $term->term_id, '_game' );
}
}
add_action( 'before_delete_post', 'delete_game_shadow_tax_term' );
I am open to suggestions