• gb.wp

    (@gregburnison)


    I have posts which have an ACF relationship to a Company CPT. I’d like to use ALM to show all the posts that are linked to a given Company. How do I limit the ALM shortcode to a certain company?

    I’ve added

    acf=’true’ acf_field_type=’relationship’ acf_field_name=’company_id’

    to my shortcode, but I’m not sure how to pass in a company_id value to match against.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    @gregburnison The ACF extension for ALM allows you to infinite scroll the items in your relationship fields, not query against the values in the fields.
    https://connekthq.com/plugins/ajax-load-more/examples/advanced-custom-fields/#relationship

    What you’re asking sounds more like a simple custom field query that you would use ALM for, not the ACF extension.

    Let me know if i’m wrong.

    Thread Starter gb.wp

    (@gregburnison)

    Hi @dcooney,

    I got this working with (as part of a line of PHP)

    meta_key=’company_id’ meta_compare=’LIKE’ meta_value=”.$company_id.”

    My problem is ACF stores the IDs in a serialized array and I’m getting matches for ID 123 of 123 AND 1234. I’m not having any luck passing quotes in the meta_value.

    Is there a way I can get just company_ids of 123 and not 1234 as well?

    Plugin Author Darren Cooney

    (@dcooney)

    @gregburnison Unfortunately, using a LIKE query will get all the values containing that.

    You may need to come up with a different solution for your use case.
    Your best bet is to likely turn your companies into a taxonomy and query by taxonomy term.

    Thread Starter gb.wp

    (@gregburnison)

    So there is no way of doing the query like ACF has in their examples and using quotes to eliminate this case?

    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    $doctors = get_posts(array(
    'post_type' => 'doctor',
    'meta_query' => array(
    	array(
    		'key' => 'location', // name of custom field
    		'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
    		'compare' => 'LIKE'
    	)
    )
    ));
    Plugin Author Darren Cooney

    (@dcooney)

    Yes, You can use this exact query with Ajax Load More, you don’t need the ACF extension for meta queries.

    The ACF extension is for infinite scrolling sub_field content, not running a query against the DB.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘ACF relationship with certain item’ is closed to new replies.