Store multiple info with update_options and retrieve it again

Hey,

The issue at hand is very basic, but I just can't quite make it seem to work.

I have two values that I want to store to my wp_options table under the same option name.

1) An array
2) A date

How can I add both values to, let's say, update_option('example', 'value'); and then retrieve them afterwards?

Since 1) is an array, I guess the data will have to be sterilized?

I would love a working example.

Sincerely,
Mika

  • Adam Czajczyk

    Hey Mika,

    I hope you're well today!

    Assuming you wish to store two values inside a single record, you'll need to "mix" this values into a single array.

    Let's say we've got this:

    $optionA = array('data1", "data2", "data3");
    $optionB = 'hello world';

    The easiest way to achieve what you wish here would be to put both this variables into one array like this:

    $optionA = array('data1", "data2", "data3");
    $optionB = 'hello world';
    
    $myoptions = array('optionA' => $optionA, 'optionB' => $optionB);
    
    update_option('example', $myoption);

    This will save both options into one record ("example") and an "update_option()" function should automatically serialize data "on the fly".

    You can then read this data the standard way and once done do something like this to separate it back to two options:

    $optionA = $myoptions['optionA'];
    $optionB = $myoptions['optionB'];

    I hope that helps!

    Cheers,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.