Support

Account

Home Forums Backend Issues (wp-admin) How Do I Further Constrain a "Filter by Post Type" Result Set

Solving

How Do I Further Constrain a "Filter by Post Type" Result Set

  • Greetings. Suppose I created two WordPress custom post types: ‘A’ & ‘B’. These are defined and registered in ‘functions.php’, and corresponding WordPress single post templates ‘single-a.php’ and ‘single-b.php’ are created.

    Next, I created ACF custom field groups for the ‘A’ and ‘B’ custom post types. The nature of the relationship between ‘A’ and ‘B’ is that one ‘A’ custom post type post associates with zero-to-many ‘B’ custom post type posts. Therefore, the ACF custom field group for ‘A’ includes an ACF ‘B Link’ custom field of type ‘relationship’. Its ‘Filter by Post Type’ value is ‘B’, which is on the ‘many’ side of the relationship. To reflect the zero-to-many cardinality rule, both the ‘Minimum posts’ and ‘Maximum posts’ values are left empty.

    The ‘A’ ACF custom field group has a single ‘Location’ rule defined as follows:

    Show this field group if ‘Post Type’ ‘is equal to’ ‘B’

    The ‘A’ custom post type’s post editor window includes this relationship ACF field, but it displays ALL posts of type ‘B’. This makes sense, because this is precisely what the ‘Location’ rule asserts. However, I need to further constrain this ‘Location’ rule with a second rule that asserts the ‘A’ custom post type post editor ‘B Link’ ACF custom field ONLY lists those ‘B’ posts that relate to a given ‘A’ custom post type post. In order words, I need a second ‘Location’ rule that is the equivalent of a SQL WHERE clause.

    By way of example, let’s use the old standby: Company & Products.

    Acme, Inc. manufacturers the following:
    – Gizmo Trampoline
    – Gee Whiz Slip-n-Slide
    – Rocket Booster Toy Water Cannon
    – Johnny B. Goode Toy Guitar

    Bonzo Toys manufactures the following:
    – Ace Ventura Pet Detective See & Discover Kit
    – Slash Toy Guitar
    – Family Guy’s Ball In A Cup

    The key here is that each product is unique to a single manufacturer. This scenario intentionally does not account for a product that is co-manufactured by two or more companies.

    By way of example, currently, the ‘Manufacturer’ custom post type editor’s ACF ‘Product Link’ relationship field is pre-populated with ALL products. My goal is for this field to pre-populate ONLY with those products produced by its linked manufacturer.

    Your feedback is appreciated. Thank you.

  • Is there some reason that you need to select from a list of products on the manufacturer field?

    For example, there are 10 products related to the manufacturer but you only want to select 5 of them.

    Or do you want to be able to have all of the products related to the manufacturer selected in the relationship field?

    If it is the second, that is all of the products are selected that are related to the manufacturer, the I would use https://wordpress.org/plugins/post-2-post-for-acf/, this plugin handles one to many relationships.

    If it is the first then you need to create an acf/fields/relationship/query filter. In this filter you need to do a query to get all of the products related to the current manufacturer. Then you use the return or this query to set the “post__in” argument for the query that ACF will do to get the list of posts to display in the relationship field.

  • Thank you for the reply, John. My short-term goal is the latter (i.e., all selected products “are related to the manufacturer”). However, I plan to implement the former as well, which is to subselect a list of products from the manufacturer field.

    While I appreciate having the “post-2-post-for-acf” plugin available, I want to avoid a solution that adds another plugin to my site. Interestingly, the link you presented for “acf/fields/relationship/quaery filter” reads like it can address both goals. Specifically,

        // Restrict results to children of the current post only.
        $args['post_parent'] = $post_id;

    Yet, I am unsure if this ACF WP filter hook manifests within the custom post type’s editor window or only in the rendered HTML, which may be why you introduced the “post-2-post-for acf” plugin.

    I would appreciate your thoughts on this. Thank you.

  • The acf filter I linked to only works in the admin when editing and has no affect on the front of the site.

    You can build your own bidirectional fields without a plugin, which is what I generally do, there are examples of this in my repo here https://github.com/Hube2/acf-filters-and-functions

  • The acf filter I linked to only works in the admin when editing and has no affect on the front of the site.

    I think we are saying the same thing. Allow me to clarify. I want to modify the behavior of an ACF custom field of type ‘relationship’ on the admin side of WordPress when it populates in the target WordPress custom post type’s editor. I will handle how this data renders on the front end by coding the WordPress single-page template that I created for the custom post type. Does this clarify my goal?

    Thank you, John, for providing a GitHub link to your work.

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

You must be logged in to reply to this topic.