Home Forums General Issues Saving dynamically-generated data


Saving dynamically-generated data

  • Hello,

    I’m working on a site right now where there are a lot of tags attached to each post. However, I want to only show some of them (the rest are being used for making recommendations around related pages and for general grouping, so not really consumer-facing).

    My thought was to build an ACF custom post type that essentially duplicated the post’s tag list, but with checkboxes in front of them. From the post admin page, the user could then check or uncheck the tags to determine which tags they wanted on the front end.

    My issue is trying to figure out how to save those checked and unchecked items, as they’re dynamically generated. Does anyone have any thoughts?

  • You’re going to need to tell me how the tags are generated in the first place, but my initial reaction is

    you have an options page with a field where these tags are selected, that saves the values. Then on the post edit page you use these values to filter what is shown in the same type of field for posts.

    Like I said, just a very rough idea, it would depend what the “tags” are (do you mean the WP tags taxonomy?) and how these values are dynamically generated?

  • Yep — I mean the normal WP tags taxonomy. Here’s how it looks within the admin:

    You can see the normal tag list on the top, then my component with checkboxes below. You’ll see that I unchecked “Plants 101” and “Relaxation Line”, so that (once it’s working) those wouldn’t be displayed on the front end.

    I’m just not sure how to save that because those tags are being generated automatically using the following code:

    $post_id = $_GET['post'];
    $tags = wp_get_post_tags($post_id);
    foreach($tags as $tag) {
        echo '<input type="checkbox" name="ppjtags_'. $tag->term_id .'" checked>'. $tag->name .'<br>';

    Does that help clarify things?

  • I think I understand what you mean.

    On the front end of the site, when the post is displayed, either the full article or the excerpt, you only want the selected tags to be shown for that post.

    These are usually shown using the function get_tag_list()

    There is no easy way to filter what this outputs.

    Basically, what you’re going to need to do is replace this function. You can can create a filter

    add_filter('the_tags', 'my_replace_tags');
    function my_replace_tags($html) {
      // generate your own html for only the tags you want to show
      $html = ????
      return $html;
  • Yep — that’s exactly right.

    Displaying the data isn’t really a concern, though — I can figure that out. Do you have any recommendations on the best way for me to save which boxes the admin has checked/unchecked?

  • I would probably use an ACF field stored as part of the post rather than my own custom field in a widget. I’d probably also remove or hide the standard WP tag box and add that as a custom field as well. Both of these fields would be taxonomy field for tags. One of the fields would save and load terms while the other does not. Then when you work on the front end I’d use the values saved in the one that does not save and load terms to do my displaying of tags.

    The main reason that I use ACF is to avoid custom coding custom fields in the WP admin.

  • I tried using a taxonomy field from ACF, but it wasn’t picking up my taxonomy as an option from the Custom Post Type that this post is. Is there a way to get taxonomies from Custom Post Types?

    Also, if I’m understanding your suggestion correctly, I’d have to add the tags twice: to the full list field, and to the publicly-displayed field. I was hoping to avoid that redundancy by having the ACF field reference the list of tags already assigned to the post.

  • what version of ACF are you using? It should be picking them up and showing them under Post Taxonomy.

    There is a way you can avoid re-selecting the same tags, but it will require saving the post twice.

    You can dynamically set the choices of a checkbox field the same way that is described here But like I said, for this to work you’d have to save the post and then on the second load you’d see values.

    In the filter you would get the tags associated with the current post and populate the choices array.

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

You must be logged in to reply to this topic.