Support

Account

Home Forums General Issues Merging a Post Object and Taxonomy into a Select

Solving

Merging a Post Object and Taxonomy into a Select

  • Hello all,

    I posted this as a reply the other day here but decided a new thread is better.

    My site uses the category taxonomy and posts to create a pretty standard hierarchy. One ACF field – a select probably is best – needs to have all the services to choose from – but – some are posts, others are categories.

    So, I need this field to be a combination of those two fields, and in alphabetical order.

    It seems that the Post Object for the posts and Taxonomy for the categories are the right choices, now I just need to merge them into one select. (This field will be on the options page in case that comes up. The data will be used on many different posts, and archive pages, that’s why it’s in my ACF Options page.)

    I am trying the code John shared in that other thread:

    $all = array_merge(
    get_field(‘relationship_1’, false, false),
    get_field(‘relationship_2’, false, false));
    $args = array(
    ‘post_type’ => ‘speakers’,
    ‘posts_per_page’ => -1,
    ‘post__in’ => $all,
    ‘orderby’ => ‘title’
    )
    $speakers = new WP_Query($args);
    // post loop
    But, trying I’m unsure how to go from
    $speakers = new WP_Query($args);

    to the new field of merged fields. Do i instead assign the merged array $args into a new ACF select field? I probably create that field, and assign the array to it?

    I think I need one of these but not totally sure:
    add_action('acf/render_field/name=merged_field',
    or
    add_action('acf/save_post'

    Thanks!
    Ben

  • You cannot merge the results of a post object and a taxonomy field. Posts and terms in WP are two different types of objects and handled completely differently.

    You need to look for a way to deal with the posts and terms separately.

  • Thanks John.

    Do you have any ideas or guidance as to where to start on that? And it doesn’t even have to be a select, I just need to have a list that they can choose from that has those categories and those posts. This user will have no knowledge of which is which.

    If I absolutely had to I could use JavaScript to do something but it definitely would not be a best practice.

  • You could do something like this https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/ but your select value would have to have some why to know if the selection is a post of category.

    For example you could use
    value: p-$post_id
    label: Post Title

    AND

    value: t-$term_id
    label: Term Name

    Then loop over the selected values to display, parse the value to figure out if it for a term or post and then do the appropriate thing for that object type.

    This would cause a significant loss in performance.

  • I will give that a try and report back. A performance loss, in this case, won’t be a huge problem. This field will used maybe a few hundred times in a whole year. The data saved from the field will be used on pages, so more often than that of course.

    Each saved field from this will be used to show its name in a div on various pages. The only other data I’ll need is finding the category’s permalink and its parent category (if it has one), and if it’s a post, get its permalink, name, and parent category as well.

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

You must be logged in to reply to this topic.