• Dave Loebig

    (@pacesettermedia)


    I’m trying to filter a Query Loop Block with an ACF User field group.

    • A custom post type of “company” is added.
    • In the user profile, with an ACF user custom field group, users can select which of the “company” CPTs they want to see on a “View Company Info” page.
    • On the “View Company Info” page, a query loop block needs to be filtered by the current user’s “Company” CPT list and display only the companies the user selected on their profile.

    I’ve found multiple pages about ACF user custom fields, query loop block variations (filters), and related  development. For my purposes, it’s a fragmented collection of how-to, none of which is what I need completely.

    • Does anybody know how to do this?
    • Or what keywords to look up to get there?
    • Is there any documentation that exists for this?
    • Any plugin specifically filtering by a user ACF, not by an ACF of the post type being displayed?

    I imagine it’s custom code to get the user’s selection of companies and return an array of IDs, and then use that array as a filter for the query loop block.

    I hope that describes the issue well enough.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I moved your topic for you. TBH which forum a topic appears in wouldn’t affect visibility to the right people much. Most of those who could be helpful use the “Topics with no replies” view which includes all sub forums. All the same, placing topics in appropriate forums helps keep things organized.

    Query loop queries still pass through the “pre_get_posts” action, so you can alter the related query vars there in order to get the desired results. The trick would be to identify this specific query from all others which pass through the same action hook. Often there’s a unique set of query vars distinguishing the query. Or use a “just in time” approach where the action callback is added just before it’s needed. The callback can then remove itself from the call stack so it doesn’t adversely influence other queries.

    Thread Starter Dave Loebig

    (@pacesettermedia)

    @bcworkz Thanks for moving the topic. And thanks for tips of what to research and try.

    bvbaked

    (@bvbaked)

    To get information from a user, there are two possible methods. The first would be to use the WordPress get_user_meta() function since the ACF fields get written into the usermeta table. This is fine for simple meta fields like a text field or select. If it is something more involved, you may want to use the ACF specific functions instead as they offer an extra layer of formatting. In this case you would want to prefix the user id with ‘user_’ which then targets usermeta.

    $text = get_user_meta( 1, 'text', true );
    $text = get_field( 'text', 'user_1' );

    Those would be an example of the two methods for getting the value of a field called ‘text’ from the user with the ID = 1

    Sounds like you’d be getting a post ID (or IDs) of a company post type, which you can then modify your query to get the assigned companies only. As @bcworkz mentioned prior, this would need to be added to a pre_get_posts hook on that specific query. You can find/edit the queryId by viewing the code editor view of your page. The block will start something like

    <!-- wp:query {"queryId":18, "query": {...} -->

    Hope this helps some

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Filtering Query Loop Block with ACF User Field’ is closed to new replies.