PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Wrong category in list

  1. Hello,

    Just purchashed the gravity form (very awesome btw) But I nog added a list to my form filled with post. like explained on http://www.gravityhelp.com/forums/topic/automatically-populate-a-dropdown-with-post-titles

    It seems to word fine however the post which are shown are from the wrong category! When you to to http://www.geneeskundebaan.nl/demo/solliciteer/ the first field is supposed to load the post from category "vacatures" but is showing post from the category "portfolio" instead.

    I use this code in mij functions.php:

    '//Adds a filter to form id 14. Replace 14 with your actual form id
    add_filter("gform_pre_render_1", populate_dropdown);
    function populate_dropdown($form){

    //Reading posts for "Business" category;
    $posts = get_posts("category=vacatures");

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding post titles to the items array
    foreach($posts as $post)
    $items[] = array("value" => $post->post_title, "text" => $post->post_title);

    //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
    if($field["id"] == 35){
    $field["type"] = "select";
    $field["choices"] = $items;
    }

    return $form;
    }'

    Posted 13 years ago on Monday April 25, 2011 | Permalink
  2. What happens when you use the category ID instead of the slug? If you look at the get_posts page in the WordPress Codex you'll notice that they're only using an ID in the examples.

    Posted 13 years ago on Monday April 25, 2011 | Permalink
  3. Thanks!, I used the documentation in the wordpress codex. This was the sulution:

    add_filter("gform_pre_render_1", populate_dropdown);
    function populate_dropdown($form){
    
    $args = array(
        'numberposts'     => 5,
        'offset'          => 0,
        'category'        => 1,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    
        //Reading posts for "Business" category;
    $posts = get_posts( $args );
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "", "value" => "");
    
        //Adding post titles to the items array
        foreach($posts as $post)
            $items[] = array("value" => $post->post_title, "text" => $post->post_title);
    
        //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 35){
                $field["type"] = "select";
                $field["choices"] = $items;
            }
    
        return $form;
    }
    
    ?>

    As you can see now its easy to order, and to set the max amount of items..

    Posted 13 years ago on Monday April 25, 2011 | Permalink