WordPress.org

Ready to get started?Download WordPress

Forums

Pass form values to query_posts (6 posts)

  1. mariostella
    Member
    Posted 3 years ago #

    I am at a loss, and I'd really appreciate a pointer.

    I am trying to filter my posts using query_posts. The posts should be filtered by category. I am using an array to only return posts that belong to all the categories at once. I got this part working.

    The problem arises when I implement a form with 4 checkboxes, one per each category, which the user should select, then submit and see a list of posts that only belong to the selected categories.

    So this is my not working code so far:

    The HTML
    [Code moderated as per the Forum Rules. Please use the pastebin]

    The problem is that nothing is returned. The URL of the page does change including the selected checkbox value, but I dunno if that is worthless. When I try to echo $cats, I only get three commas, no category names, or values of sort.

    Do you have an idea if I am passing the variables the wrong way or anything else? Thanks in advance.

  2. alvaron
    Member
    Posted 3 years ago #

    Maybe You should try something like this:

    working with the category name

    $catnam = $_GET["catnam"]; // or $_POST["catnam"];
    query_posts("category_name=$catnam");

    working with the category id

    $catid = $_GET["catid"]; // or $_POST["catid"];
    query_posts("cat=$catid");

    Hope it helps you.

    Alvaro

  3. mariostella
    Member
    Posted 3 years ago #

    Hi, thanks Alvaron

    Probably you replied after the mod removed my code (@mod: sorry for the slip by the way.)

    So here ya go with pastebin: http://wordpress.pastebin.com/EQSPiFc0
    Hope this makes things clearer for you.

    I saw your post on your blog and the issue is not how to filter posts from one category or multiple, either getting posts from all those categories or just those that belong to all the categories at the same time, as that would be
    query_posts('cat=1,2,3,4');
    and
    query_posts(array('category__and'=>array(1,2,3,4),'showposts' => -1)); ?>
    respectively.

    My issue is: I want to use the latter function (so get posts that belong to multiple categories at the same time), but I want users to select what these categories should be from a number of checkboxes. I have no idea how to pass the values of the checkboxes to the friggin' array!! It is driving me nuts (of course I am a total rookie at this, so...)

    I will try and see if your suggestion can help in any way though!

    Obrigado!

  4. mariostella
    Member
    Posted 3 years ago #

    @alvaron No luck at all with your advice. I must be missing the proper way of formatting the form data. I am not sure I am doing it correctly. Could you please take a look at my pastebin data and see if there are macroscopic errors I should correct? I have tried various combinations with id, name and value, but nothing works.

  5. mariostella
    Member
    Posted 3 years ago #

    Sorry for bumping, but I am still, well, bumping my head against the wall over this.. anyone please?

  6. shaile
    Member
    Posted 2 years ago #

    You have to use $catid like..............>>>

    $catid = $_GET["catid"]; // or $_POST["catid"];
    query_posts("cat=".$catid);

Topic Closed

This topic has been closed to new replies.

About this Topic