• Resolved fitmasu

    (@fitmasu)


    Is there a way to fetch a WordPress Data (such as article or product list) in to a Forminator’s Dropdown or checklist automatically? I usually want to build a form containing these features for various reasons such as (example 1) Ask for a review from a specific article on a complaint form or (example 2) A custom order form for a product from a product list.

    Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @fitmasu

    I hope you are doing well

    You can give this a try in this code: https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b

    I tested it and still working well in the latest version of Forminator, you just need to replace your form ID, select ID and Post type

    Add it as a mu-plugin https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Best Regards
    Patrick Freitas

    Thread Starter fitmasu

    (@fitmasu)

    I’ve added the MU plugin and replaced the Form ID, but what’s ‘Select ID’ and ‘Post type part’? Also, how does it communicate with Forminator? What do I put in ‘URL Parameter Key’?

    Thank you!

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @fitmasu,

    Please refer to line 11:
    'select-3' => 'post',

    In this example, “select-3” is the name of your Select field, which you’d like to be populated with your data.

    post” here refers to the regular posts. In order to populate the field with different post types, you’ll need to replace “post” with the custom post type slug. Usually you can find it in the address bar after “post_type=” when viewing the list of the custom posts (such as pages, articles, etc.) in the backend:

    https://prnt.sc/BdXg83jVhJAM

    The must use plugin run and automatically trigger the function, once “forminator_cform_render_fields” filter hook is triggered, which happens when the form fields are being rendered.

    Hope this helps. Let us know if you have any further questions!

    Best Regards,
    Dmytro

    Thread Starter fitmasu

    (@fitmasu)

    Thank you.. What do I put in the Select field’s ‘Query Parameter Key’ field?

    https://prnt.sc/QSRM5nKhPfUP



    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @fitmasu

    For this code to work you do not need to put anything in that setting.

    The “Query parameter” in “Pre-populate” setting is for a different use: when you want to fill-in the field (or select option in case of “select” or “radio” buttons) based on the URL.

    It’s in no relation to this case and shared code and you just leave it empty as is.

    Best regards,
    Adam

    Thread Starter fitmasu

    (@fitmasu)

    It worked! Thank you so much!

    Two minor questions:

    1. If I want another form to do a similar thing on the same website, do I duplicate the file/duplicate the code in the same file and it will work?
    2. Currently, it only fetches the recent 5 posts, is it possible to extend the list? Couldn’t seem to be able to find that in the code.

      I wish if this was possible out of the box in the future, so it can communicate with CPTs and other Forminator-standard details without custom code dependence.
    • This reply was modified 10 months, 3 weeks ago by fitmasu.
    • This reply was modified 10 months, 3 weeks ago by fitmasu.
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @fitmasu

    I’m glad it works!

    If I want another form to do a similar thing on the same website, do I duplicate the file/duplicate the code in the same file and it will work?

    You shouldn’t duplicate the code as it would cause fatal errors due code conflict. The code should (and can) be modified a bit to support multiple forms. If you ever need help with this, let us know and we’ll try to assist with that.

    Currently, it only fetches the recent 5 posts, is it possible to extend the list? Couldn’t seem to be able to find that in the code.

    Code would fetch the “default” number of posts (which is 5 for “get_posts()” function) but you can change it in this line:

    $posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );

    Example modifications:

    a) to fetch 10 posts use this instead

    $posts = get_posts( array('post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ],'numberposts' => 10 ) );

    b) to fetch all posts use this instead

    $posts = get_posts( array('post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ],'numberposts' => -1 ) );

    Best regards,
    Adam

    Thread Starter fitmasu

    (@fitmasu)

    Thank you very much for your above and beyond support, even when this is not a stock issue. I really appreciate it.

    I tried both codes and didn’t work. I have some 20 posts in the post type I’m trying the code with, but still fetches only 5. Could there be anything limiting this from working?

    https://prnt.sc/2cnw-me4ElQx

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @fitmasu,

    Hope this message finds you well.

    I tested the code and I was able to get all the posts without any coding.

    Still, the ‘numberposts’ => 5, didn’t work since I tried to set it to the lowest value.

    But this is not related to Forminator, since the code is using the get_posts() function, which is a WordPress core function, you can find more information about this here:

    https://developer.wordpress.org/reference/functions/get_posts/

    Best regards,
    Laura

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Post Data in a Checkbox/Dropdown List’ is closed to new replies.