0

The wp_load_alloptions() function in option.php file, loads all options if no options is set autoload=yes Why does it do that?

function wp_load_alloptions( $force_cache = false ) {
global $wpdb;
... remaining code

if ( ! $alloptions ) {
    $suppress      = $wpdb->suppress_errors();
    $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" );
    if ( ! $alloptions_db ) {
        $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
    }
    $wpdb->suppress_errors( $suppress );

    ... remaining code

2 Answers 2

1

Following the history on GitHub, it appears that this behaviour has existed since wp_load_alloptions() was introduced, 17 years ago. It was added as part of code that was introduced to address this ticket, and seems to be intended to fix issues related to object caching.

The full explanation for the entire change is given as:

(In [4855]) Introduce Notoptions and Alloptions caching, so that all options (and previously attempted Notoptions) are read from the cache in one go. Should reduce cache misses to zero or close to it. fixes #3726

To explain this a bit:

  • Two pseudo-options are stored in the object cache (just there, not in the DB)
  • 'notoptions' stores all attempted non-options so that the DB isn't accessed more than once trying to load these options that don't exist
  • 'alloptions' stores all autoloaded options. The first time any option is queried, 'alloptions' will be populated (either from the DB, or from a persistent object cache). If the option is in there (likely, most options are autoloaded), it'll be read from there. So you just have one cache read for all your autoloaded options.
  • Non-autoloaded options revert to the options cache as before (one item per cache entry).
  • Since the loading of the autoloaded options is used by get_option(), people using alternative object caches will no longer see individual queries for each option on the first load (i.e. with an empty object cache)

All of this should be transparent. As long as you use get_option(), add_option(), update_option(), and delete_option(), it should be like nothing ever happened.

This should reduce cache misses to zero (or close to it), and it should reduce redundant queries.

1
  • 1
    Got it now, thank you!
    – p4atik
    Commented Oct 11, 2023 at 3:54
1

The comments on the wp_get_alloptions() function say that it Loads and caches all autoloaded options, if available or all options. If there are autoload options, the function will fetch them; however, if (and only if) no autoload options are found, all options are fetched.

If you're asking why is this done in the sense of Why is WordPress designed this way, you might need to start a thread in the WordPress support forums.

1
  • 1
    Yes, I read that in the comments but couldn't understand why it was done that way, will ask on the support forum
    – p4atik
    Commented Oct 11, 2023 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.