• Resolved skotperez

    (@skotperez)


    Hello,

    I’m building a front-end form to add a new post of a particular post type, let’s say ‘book’. This pod has a relationship field that links it to a second post type, let’s say ‘library’.

    In the dashboard i’ve limited the list of libraries in the relationship field using the where clause in the Relationship Options tab, like this: t.ID=43, where 43 is the desired library ID.

    When I try to do the same using an array of options for this field, including the where clause, it is not working:

    $my_pod = pods('my_pod');
    $fields = array(
    'related_library' => array('where'=>'t.ID=43')
    )
    $my_pod->form($fields);

    I’ve read the documentation pages about form method, about find method and the find notation options. Is “where” clause available into the form() method? If not, is there any other way to filter or limit relationship fields in front-end forms?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter skotperez

    (@skotperez)

    Just a clarification to the previous message: I know that the where clause that can be defined in Relationship Options tab has effect in the front-end form too. But I need to define a dynamic where clause: I need the ID to be variable depending on the connected user.

    Plugin Support Paul Clark

    (@pdclark)

    <?php
    add_filter(
    'pods_pods_find',
    function( $params ) {
    $params = (array) $params;
    if ( array_key_exists( 'where', $params ) ) {
    $params['where'] = str_replace(
    'CURRENTUSERID',
    get_current_user_id(),
    (string) $params['where']
    );
    }
    return (object) $params;
    }
    );

    …where this filter will change the WHERE query to whatever value is assigned to $params['where']

    In the above example, if ( array_key_exists( 'where', $params ) ) { causes the change to only operate if there is some kind of WHERE query set, and the change that happens is text CURRENTUSERID is replaced with the ID of the currently logged in user.

    For more refined user-based logic or changes to what data relationship fields display, there is also the pods_pick_field_data filter.

    Thread Starter skotperez

    (@skotperez)

    Hello, Paul,

    thank you so much for this awesome solution and the quick reply. Is there any place where I can consult this documentation? If not, let me know if I can help with that.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.