• Trying to figure out how to display certain categories based on the answers of a simple 5 question radio-button form?

    So, for example, the user selects 3 out of 5 things they’re interested in from a fixed number of categories (let’s say cats, dogs, birds) from a simple form, then posts from the categories-cats, dogs, birds are displayed.

    I know I’ll have to associate the categories and the form, just not sure where to begin. Any ideas out there on references or where to begin?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m assuming your going to use checkboxes and a submit to cause the action of refreshing.

    Give each checkbox a name attribute with the category your wanting to filter through.
    <input type="checkbox" name="cats" value="123" /> Cats
    // 123 is the id of the category.
    Then before the form, check to see if $_POST has a value for those
    fields.

    <?php
    $filter = array();
    if ( isset($_POST['cats']) ) {
    	$filter .= $_POST['cats'];
    }
    $categories = implode(',', $filter);
     ?>

    [form stuff]
    <?php $my_posts_loop = new WP_Query(array('post_type' => 'post', 'cat' => $categories ));?>
    [loop stuff]

    If you’re trying to do it on a built in page, like the blog index, then you need to update the query_vars through a filter/actions — not sure which it is… pre_get_posts or something.

    Should get you started and there might be a typo or something in there. Heading out the door.

    $filter .= $_POST[‘cats’];
    should be
    $filter[] = $_POST[‘cats’];

    and add an !empty($_POST[‘cat’]) to the if statement

    Thread Starter C1D

    (@c1d)

    Thanks Dankicity, really appreciate your input! Going to begin here and see what I come up with…I’ll post all the progress for further troubleshooting!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display Categories based on Form’ is closed to new replies.