• Hello, I’m trying to create a “compare” sort of function on a website I am building. I have a compare.php file that takes all posts from a custom post type and lists them inside a form with each post having a checkbox. What I’d like to do is make it so that when the user submits the form, a new page will load that will build a query using the ids from the posts that were checked off.

    The basic outline of the code I wrote on the compare.php file is something like this.

    <form id="compare-form" action="<?php bloginfo('url'); ?>/compare-results" method="get">
    
    // query to get the custom post type is here
    
    // The loop includes a bunch of information then at the end a checkbox that makes the name equal the id for that post, like this:
    
    <input type="checkbox" name="<?php echo get_the_ID(); ?>" value="">
    
    <input type="submit" id="searchsubmit" value="Compare" />
    </form>

    I then have built a compare results template that has a custom query using these args, but I can’t figure out how to get the post ids from the previous page into this query.

    $args = array(
    	'post_type' => 'customposttype',
    	'post__in' => array( how do I get the post ids from the checked boxes into this array?? )
    	);

    I hope this make sense. In the end it should build a query using only the posts that were checked off on the previous page.

Viewing 1 replies (of 1 total)
  • Thread Starter jvwekken

    (@jvwekken)

    In case anyone is looking for something similar, here is what I ended up doing.

    First, I changed the method on the form to “post”. Then inside the loop I used this for the checkbox:

    <input type="checkbox" name="comparelist[]" value="<?php echo get_the_ID(); ?>">

    Then in my results php file I used this code

    $comparelist = array();
    foreach($_POST['comparelist'] as
    $listitem) {
    	$comparelist[] = $listitem;
    }

    …and this as the arguments for the query.

    $args = array(
    	'post__in' => $comparelist
    	);
Viewing 1 replies (of 1 total)
  • The topic ‘Creating custom query from form data’ is closed to new replies.