Search code examples
javascriptphphtmlwordpresscustom-wordpress-pages

Is it possible to use PHP to compare two posts and when the title matches, get the ID of the post?


First a bit of context to understand what I'm doing. I'm using WordPress. So I have a page with a list of texts in Latin. These items (the titles and excerpts) are automatically posted on the page (sort of like a blog page), and the texts themselves are posts in the WordPress back-end. I have a category "text" and a category "translation" to separate the two types of content.

When I click on one text, I get sent to a detail page where the full text can be read. When the link is clicked, the ID of that post is stored in a $GLOBALS variable. That variable is then called on the detail page to show the entire content of the text. I used an AJAX call to store the variable. And this is how I post the contents on the detail page:

<div class="left">
 <?php
    $post_id = $GLOBALS['post_id'];
    $queried_post = get_post($post_id);
    $title = $queried_post->post_title;
    $content = $queried_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
 ?>
 <h3><?php echo $title; ?></h3>
 <p><?php echo $content; ?></p>

What I want to do is show the translation of each text on the same detail page in a second column (the css is no problem). I tried a few things like this:

<div class="right">
 <?php
    $post_id_two = ???
    $queried_post = get_post($post_id_two);
$title_two = $queried_post->post_title;
    $content_two = $queried_post->post_content;
    $content_two = apply_filters('the_content', $content);
    $content_two = str_replace(']]>', ']]&gt;', $content);
 ?>
 <h3><?php echo $title_two; ?></h3>
 <p><?php echo $content_two; ?></p>

But I just can't figure out how to compare the second post title to the first one and the show the correct translation on the corresponding detail page. If there is an easier way to do this, anything is welcome.


Solution

  • I think an easier approach to this might be to add a custom meta box that allows you to store the post ID of the translation text as meta data to the post with the Latin text. Here's a kind of crude way to do it, but you'll at least get the idea:

    Add this to functions.php in your theme or to a plugin. (a plugin is preferred for this type of stuff)

    //Define the contents of your meta box
    function prefix_meta_box_contents($post){
        $current_value = get_post_meta($post->ID, 'prefix_translation_post', true);
        ?>
        <input type="number" value="<?php echo $current_value; ?>" min="0" name="prefix_translation_post">
        <?php
    }
    
    //Save the meta data
    add_action('save_post', 'prefix_save_translation_post_meta');
    function prefix_save_translation_post_meta($post_id){
        if(array_key_exists('prefix_translation_post',$_POST)){
            update_post_meta( $post_id, 'prefix_translation_post', $_POST['prefix_translation_post'] );
        }
    }
    
    //Hook onto the add_meta_boxes action
    add_action('add_meta_boxes','prefix_meta_box');
    function prefix_meta_box(){
        //Register the box
        add_meta_box('prefix_translation_post_mb','Translation Post','prefix_meta_box_contents');
    }
    

    Then go to your Latin text post and tell it the ID number of the post that contains the translation. Save that, then try using this for your translated text column:

    <div class="right">
     <?php
        $post_id_two = get_post_meta($queried_post->ID, 'prefix_translation_post', true);
        $queried_post_two = get_post($post_id_two);
        $title_two = $queried_post_two->post_title;
        $content_two = $queried_post_two->post_content;
        $content_two = apply_filters('the_content', $content);
        $content_two = str_replace(']]>', ']]&gt;', $content);
     ?>
     <h3><?php echo $title_two; ?></h3>
     <p><?php echo $content_two; ?></p>