Support

Account

Home Forums General Issues Is my field/data structure correct?

Solved

Is my field/data structure correct?

  • I have a possibly ambiguous question. Let me start out by saying I don’t have any programming training. I’ve taught myself enough to be dangerous, but I still haven’t gotten to the point of being able to “think like a programmer” and visualize the best solution for a given scenario.

    I have a few questions, one of which concerns my field or data structure, for lack of a better term. Let me quickly explain my current project:

    There is a custom post type for Veterans. For this post type I created a number of custom fields using ACF (which is awesome… I love this plugin!). The two fields that are pertinent here are State and County. When creating a new Veteran, the user selects the value for State: currently they have the options of Iowa, Minnesota, South Dakota, and Wisconsin. Then they select what County they are from. To avoid county name conflicts, I created four County fields (IA County, MN County, etc.). In the field options I gave each of these fields the logic of only being available when their respective state is selected as the value of the State field.

    Doing it that way gets the job done, but it just doesn’t feel right to me. I feel like, ideally, there would be one table including all counties, each county with a unique ID, with a foreign key linking each county with their respective state.

    So, question 1: am I doing that part wrong, or is it good enough to get the job done?

    On to question 2.

    As a result of having four individual tables for each state’s counties, now I have to figure out what state the Veteran is from in order to get their county. When I began working on the archive page for veterans I just made a simple elseif flow (if state = mn, then county = get_field(‘mn_county’). While that still felt a little dirty to me, it got the job done. However, now I’m dealing with having to pull that same data across a number of pages.

    As I got to the third template file and pasted the same blocks of code for pulling data from my ACF fields, I suddenly realized I was not being very DRY at all! So I thought I’d be fancy and make a custom function in my functions.php file. The main problem I ran into here was not being able to get ACF field data into my functions.php file.

    So question 2 is: how can I access my ACF fields from functions.php in the context of the Loop?

    I hope all of that is understandable 🙂

    Many thanks in advance!

  • I read through this post: http://support.advancedcustomfields.com/forums/topic/how-to-call-acf-fields-from-my-functions-php-file/

    I tried using something like $state = get_field('state', $post->ID); inside functions.php, but it still returned null.

    So then I created veterans_functions.php in my theme’s root dir and included that same code, used include_once in my pages where I wanted to use those variables, and now it works. However, on my archive and index pages each veteran listed there is populated by the data of the most recently added veteran…

  • Silly mistake… on my archive pages I switched from include_once to include, and it works 🙂

    If possible, though, I’d still like to know if anyone knows how I can move these variables and functions over to functions.php and not have a separate file just for this.

  • Question one:

    That’s the easiest and fastest way to do it using fields. On the other hand, you could create a custom taxonomy that is hierarchical and then at the top level create the states and then on the second level you could have counties.

    To do it with fields so that only the counties you want are loaded would take a lot more work, you’d need to create some custom javascript to reload the county select field whenever the state field was changed. Possible, but takes longer and the result from the users perspective will be the same.

    Question 2:

    I would also carefully name my county fields. If you are using the state abbr for the value of state then:

    
    ia_county, nm_county, etc
    

    then you can simplify getting the county

    
      $county_field = $state.'_county';
      $county = get_field($county_field)
    
  • Thank you for the ideas.

    As for the first question, I’m thinking about moving all counties to one dropdown field. For the values I would use something like “martin_mn” with the label “Martin” to represent Martin County, Minnesota. With the combobox option enabled, I don’t think this would be a problem for data-entry, as the user can simply “search” for the county they want to select.

  • I’ve recently been creating a site where the client has a form with two fields. The first is populated with countries and then the second is populated with states and provinces. Rather than have something dynamic that only shows the states/provinces in the selected country the “state” select field has “Country Abbrv: State/Province”. Much the same what what you are thinking for states and counties.

  • Cool. I’m glad that wasn’t some fringe idea, then 🙂

    Thanks again for the help and input.

  • One more follow-up question:

    Right now I have a select field with options like “Martin (MN)”. The only reason I have (MN) included is so A) if there were a Martin MN and a Martin WI, these two choices would be different, and B) so the user can tell the difference while selecting counties. However, when displaying the field on my site, I only want to display the name “Martin”, and not “(MN)”. Is the only real solution here some sort of PHP string function, like somehow not displaying the last 5 characters of any county field?

    Thanks again.

  • Figured it out 🙂

    
    $county_field = get_field('county', $post->ID);
    $county = substr($county_field, 0, -5);
    
Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Is my field/data structure correct?’ is closed to new replies.