Support

Account

Home Forums Add-ons Options Page Get all fields from option page

Solved

Get all fields from option page

  • So, point is, I am using options page for theme settings. So, there “footer-left”, “footer-right” part. It has right now 5 items.

    By official documentation I would have to have in footer.php 5 rows with
    $variable = get_field("obrazek_pl", "option");

    And this 5 times … which is not making much sense – like 5 requests? No, thanks. But I can not find a solution, how to load everything from one option page in one request. There is functions get_fields(), but I can not find a way to load this, since this functions needs ID. And option page does not have one.

    Any solution? Anyone? Thanks …

  • There isn’t a way to load all of the options from a single options page in a single load.

    Options pages have the “autoload” setting. Using this means that all of the options are loaded when WP loads in a single query reducing the queries needed.

    Another option is to set the “post_id” option for the options pages to an ID of a “post”. This would let you use get_fields() to get all the field for that specific options page because the values are no longer stored in options but in postmeta. Instead of using “options” as the post ID to get values you would use your custom post ID.

  • So i.e. I have my footer.php file, how I manage to load everything from my sub-option page in admin called “footer”?

    In ACF PRO I have set two fields, “footer-left”, “footer-right”. I am loading it under sub-page in admin footer.

    Now, what is the best code option, to load everything from my “sub-page”?

  • I personally would set autoload for the options page to true and then just get each fields using get_field('field_name', 'options') or the_field('field_name', 'options')

    Or, if there are a lot of fields I would name them in a way that would let me create a loop if there was some way to standardize the output for all of them.

    
    $sfxs = array('left', 'right');
    foreach ($sfxs as $sfx) {
      the_field('footer-'.$sfx, 'options');
    }
    

    If your concern is the number of db queries made then using the autoload feature for the options page is the solution.

    If your concern is the number of times you need to type out $variable = get_field("field_name", "option"); I don’t have a solution.

    There isn’t a way to get all of the values for a specific options page in a single call. You can use get_fields('options'), but this will return all options for all ACF options pages and not for a specific page. Because the values are stored in the options table there is not association with a specific options page.

  • In order to do this you must register your option pages with separate ID’s. There’s no mention of this in the ACF Docs but you can enter 'post_id' as a parameter when you use acf_add_options_sub_page or acf_add_options_page. Then you’ll be able to use get_fields('your_own_id') to only get the fields on that particular option page. Otherwise all option pages are registered with the same id: ‘option’.

    To see what is happening you can use the function acf_get_options_pages() to list all your registered option pages and see their parameters.

  • Just curious if you could expand on this? Can you show an example of your code or how you would register the post_id to an option sub page? thank you

  • Yes, just like @elardael said. You would add this as a parameter in to acf_add_options_sub_page or acf_add_options_page.

    add_action('acf/init', function() {
    	if (function_exists('acf_add_options_sub_page')) {
    
    		acf_add_options_sub_page(array(
    			'post_id'     => 'unique_id',
    			'menu_title'   	 => 'Event Settings',
    			'parent_slug'    => 'edit.php?post_type=events',
    		));
    
    		acf_add_options_sub_page(array(
    			'post_id'     => 'another_unique_id',
    			'menu_title'   	 => 'Cookie Settings',
    			'parent_slug'    => 'edit.php?post_type=cookies',
    		));
    
    	}
    );

    This would allow you to be more certain and specific when getting fields from option pages. Rather than using get_field('field_name', 'options'); you can type get_field('field_name', 'unique_id'); and be certain that the field is from that specific options page.

    Now you can also have field names with the same name on different option pages, not possible if you don’t register a post_id.

    Also, as mentioned before you, can get all fields from an entire options page; get_fields('unique_id');

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

The topic ‘Get all fields from option page’ is closed to new replies.