Support

Account

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

Solved

Getting Custom fields from 2 different pages to compare (ajax?)

  • Hi!

    I would like to create comparison pages that use custom fields from 2 pages and use their title to create 1 page, for example for LG G4 and LG G5 the page will be called LG G4 vs LG G5.

    I have about 500 pages on the site and I want to create comparison pages for all of them but there is no need for doubles, for example I don’t need lg g4 vs lg g5 and lg g5 vs lg g4 – just one of the possible combinations.

    After that I want to create some sort of simple navigation where I will be able to choose 2 different pages on the site that using 2 drop-down menus.

    When the 2 pages will be selected and the user will press a button it will open the specification fields from each page in a table and it will be easy to compare 2 products.

    For example, if I choose LG G5 and LG G4 and press compare it should open a page with a unique URL like this: http://mysite.com/lg-g4-versus-lg-g5

    The site is being updated regularly and I don’t want to create static pages for that every time.

    Any help would be much appreciated.

    I have the latest ACF PRO.

    Thanks!

  • I found a code snippet from github that will help me create pages without actually creating them, here it is:

    // create fake page called "chat-room"
    // modify function and variable names with "ABCD" to whatever you like
    // modify variable $fakepage_ABCD_url to the fake URL you require
    
    add_filter('the_posts','fakepage_ABCD_detect',-10);
    function fakepage_ABCD_detect($posts){
        global $wp;
        global $wp_query;
    
        global $fakepage_ABCD_detect; // used to stop double loading
            $fakepage_ABCD_url = "chat-room"; // URL of the fake page
        
        if ( !$fakepage_ABCD_detect && (strtolower($wp->request) == $fakepage_ABCD_url || $wp->query_vars['page_id'] == $fakepage_ABCD_url) ) {
            // stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
            $fakepage_ABCD_detect = true;
            
            // create a fake virtual page
            $post = new stdClass;
            $post->post_author = 1;
            $post->post_name = $fakepage_ABCD_url;
            $post->guid = get_bloginfo('wpurl') . '/' . $fakepage_ABCD_url;
            $post->post_title = "Page Title";
            $post->post_content = fakepage_ABCD_render();
            $post->ID = -1;
            $post->post_type = 'page';
            $post->post_status = 'static';
            $post->comment_status = 'closed';
            $post->ping_status = 'open';
            $post->comment_count = 0;
            $post->post_date = current_time('mysql');
            $post->post_date_gmt = current_time('mysql', 1);
            $posts=NULL;
            $posts[]=$post;
            
            // make wpQuery believe this is a real page too
            $wp_query->is_page = true;
            $wp_query->is_singular = true;
            $wp_query->is_home = false;
            $wp_query->is_archive = false;
            $wp_query->is_category = false;
            unset($wp_query->query["error"]);
            $wp_query->query_vars["error"]="";
            $wp_query->is_404=false;
        }
        
        return $posts;
    }
    
    function fakepage_ABCD_render(){
        return "My amazing pretend page :D";
    }
    

    Please help me figure out how to change the url, title, and get the custom fields I want dynamically for 2 pages I choose.

    Thanks!

  • 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 🙂

  • I took your advice and with a little more time researching the I got it working the way I intended, thanks a lot!

  • Hello, I have the same problem. Could you share your code with your solution?

  • Hi James, I know this is a bit of an old topic, but this seems to be exactly what I was looking for.

    My basic template works, but I’m getting a bit stuck on how to get those post IDs into the url (basically how to get users to select the product that is to be compared and send them to the right URL with the correct Post IDs).

    Would you be able to give me some pointers of where to start looking for a solution?

    Thanks! Rob

  • Hi @city17

    Thanks for the question.

    You can learn about using query vars from this article I found on the internet https://zeropointdevelopment.com/how-to-use-url-parameters-to-pass-data-to-wordpress/

    Let me know if you need additional help. Thank you.

  • Thanks James, that helped me get started.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Getting Custom fields from 2 different pages to compare (ajax?)’ is closed to new replies.