Support

Account

Home Forums ACF PRO Save user input values per ACF post.

Solving

Save user input values per ACF post.

  • So here’s my issue. I know I’m doing something wrong.

    I have a ACF group called offers.
    We are creating a bunch of offers, and would like to give the logged in user the ability to “archive” offers they no longer want to see.
    I created a form which uses the user ID as a post_id.
    I was able to set the flag, (It’s a checkbox) but it did it for EVERY offer this user sees.
    How can I do it for one offer post or the other, but not all?

    I appreciate any guidance you can give.

    Here’s my code if it helps. (Looping the $i for incrementing form IDs)

    $options = array(
    ‘id’ => ‘archive_’.$i,
    ‘post_id’ => ‘user_’.$current_user->ID,
    ‘fields’ => array(‘field_5a88582291e40’, ‘user_’.$current_user->ID),
    ‘submit_value’ => ‘Archive/Redeem’
    );

    acf_form($options);

  • I don’t understand where you ‘show’ the offers and how the users should ‘archive’ them. Is offer(s) a custom post type ?

    For my explanation I assume it is (because to me that would be the most logical solution). If it is, I wouldn’t use an ACF form for this because this sounds like a simple user meta storage to me.

    What you want (afaik) is exclude certain (post ids) from showing to the user.
    The ‘goal’ is then to store which ids need to be excluded for each user.

    You would need to create a page which has a form where a user can select which orders/posts have to be excluded.

    The code below needs to ‘go’ into the page which has the form (where users can exclude it).

    if ( isset( $_POST[ 'order_form_nonce' ] ) ) {
    
        if ( ! wp_verify_nonce( $_POST[ "archive_order_nonce" ], 'archive-order-nonce' ) ) {
            // not verified, so return and do nothing
            return;
        } else {
            $offers = ( isset( $_POST[ 'offers' ] ) ) ? $_POST[ 'offers' ] : false;
            if ( is_array( $offers ) && count( $offers ) > 0 ) {
                foreach( $offers as $offer_id ) {
                    $offers_to_archive[] = $offer_id;
                }
                update_user_meta( get_current_user_id(), 'offers_to_archive', $offers_to_archive );
            }
        }
    }
    
    $offer_args = array(
        'post_type'      => 'offer',
        'posts_per_page' => -1,
    );
    $get_offers = get_posts( $offer_args );
    
    if ( $get_offers ) {
        ?>
        <form name="archive_orders" action="" method="post">
            <input type="hidden" name="archive_order_nonce" value="<?php echo wp_create_nonce( 'archive-order-nonce' ); ?>">
            <?php foreach( $get_offers as $offer ) { ?>
                <input name="offers[]" id="offers" type="checkbox" value="<?php echo $offer->ID; ?>" /> <?php echo $offer->post_title; ?>
            <?php } ?>
            <input type="submit" class="button" value="<?php esc_html_e( 'Save', 'text-domain' ); ?>" />
        </form>
        <?php
    }

    Then you can use a simple exclude query to exclude the post ids which are stored by the user.

    $hide_offers_user = get_user_meta( get_current_user_id(), 'offers_to_archive', 1 );

    And then extend your query with this:

    'post__not_in' => $hide_offers

    Hope that helps…

  • Thank you so much for your help!

    Yes, “offers” is a CPT.
    I show them all to the user in list on a page.
    I’d like to let the user click a button on an offer to select it to be archived, at which point the offer is removed from the page. (I haven’t decided if I want to use AJAX for that or a simple form submission.)
    My original thought was that the user meta data would store the offer’s post ID, and the archive value for that offer post id.
    Then I could filter the offers by those meta data records.

    If I understand your code correctly, I would be saving an array of offer IDs in the user meta and then use that array to exclude the offers?
    Makes sense, but may also become unruly if the list of offers become very large. Which it probably will. Which, I suppose my plan would too, since I would have new records for each archived offer.

    I’ve worked a lot with WP, but never manipulated user meta before, so this is new to me.

  • Why would you store the post id and the archived id ? Don’t you only need to filter which offers need to be excluded ?

    I don’t see that (the user meta becoming big) as a problem right now. It’s just an array of ids.

    A button is also an option. The function will almost be the same then.

    You could maybe also store the info in a transient but I’m not sure if transients are meant to be used for this, although technically I don’t see a limitation.

  • Good point. You’re right, I just need the post ID.

    My issue was that when I submitted the form, the post ID wasn’t saved to user meta, which was what I expected. It saved the archive value, but I didn’t know how to save the post id related to the form I submitted. Was thinking it was a value I could set in the submitted form, but wasn’t sure if that was an ACF setting or something I needed to add myself.

  • KISS = Keep It Stupid Simple

    That’s the way I would go 🙂

  • Did you get it to work ?

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Save user input values per ACF post.’ is closed to new replies.