Support

Account

Home Forums ACF PRO Getting Custom fields from 2 different pages to compare (ajax?) Reply To: Getting Custom fields from 2 different pages to compare (ajax?)

  • Hi @rmoses123

    The code you shared needs you to add the URLs for each possible combination, which I believe is what you don’t want to do:

    $fakepage_ABCD_url = "chat-room"; // URL of the fake page

    The easiest way to achieve it is to create a new page template for the comparison and use the URL query string to get the posts being compared. So for example, if you have a page with this URL:

    http://example.com/comparasion/?post1=99&post2=123

    Then you can get the data for each post like this:

    // check if the comparation exists
    if( isset($_GET['post1']) && isset($_GET['post2']) ){
        
        // get the post data
        $post1 = get_post($_GET['post1']);
        $post2 = get_post($_GET['post2']);
        
        // Show the first post data
        if( $post1 ): 
    
            // override $post
            $post = $post1;
            setup_postdata( $post ); 
    
            ?>
            <div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
            </div>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            
        <?php endif;
        
        // Show the first post data
        if( $post2 ): 
    
            // override $post
            $post = $post2;
            setup_postdata( $post ); 
    
            ?>
            <div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
            </div>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            
        <?php endif; ?>
    }

    I hope this makes sense 🙂