I'm working with some quiz script which is using javascript to init the script, the data structure which I working on is look like this

var quizArray = { 'contents': [{
            'question': 'First Question',
            'answers': ['First Option', 'Second Option', 'Third Option', 'Fourth Option'],
            'correctAnswer': 1
        }, {
            'question': 'Second Question',
            'answers': ['First ', 'Second ', 'Third ', 'Fourth '],
            'correctAnswer': 2
        }, {
            'question': 'Third',
            'answers': ['a', 'b', 'c', 'd'],
            'correctAnswer': 4
        }, ]

What I want to do is, to create Custom Post Meta to store the data, but since I want the user can input the data unlimited ( It may 1 question, 2 questions or more ) and if I am going to use Custom Post Meta how should I do with the custom meta field, I'm thinking to turn it into an array which contains of multiple value but don't know how it can be done with custom post meta, and later turn the array into JSON encode, and use it in javascript.


If it where me id store each question as a 'question' type meta as a stored array. Wordpress automatically serializes your array before storing it.

So then when you use get_post_meta($post_id, 'question') it will return an array of serialized strings.(I think, Im pretty sure it wont unserialize it for you if there are more than 1 results)

Then just run a foreach loop and go through each question


say I have a JSON object and I post it into PHP

$myobject = json_decode($_POST['myobject']);
$contents = $myobject->contents;

So looking at your JSON it should look something like this:

//the whole things an array

//these are your question things
array('question' => 'First Question',
      'answers'=> array('First Option', 'Second Option', 'Third Option', 'Fourth Option'),
      'correctAnswer' => 1
array('question' => 'First Question',
      'answers'=> array('First Option', 'Second Option', 'Third Option', 'Fourth Option'),
      'correctAnswer' => 1
array('question' => 'First Question',
      'answers'=> array('First Option', 'Second Option', 'Third Option', 'Fourth Option'),
      'correctAnswer' => 1
//end of the array

Now what you can do is store each one of them as meta objects for your post

foreach($contents as $question_array) {
    add_post_meta($post_id, 'question', $question_array);

Then later (like on a page) where you want to show the meta you simply call it and loop through it

$questions = get_post_meta($post_id, 'question', false);//false by default just showing here because we want an array. 
foreach($questions as $question){
    $question_array = maybe_unserialize($question);
    //now we have the question array from earlier and we can do anything like:
    echo "Question: <br>" . $question_array['question'];
    echo "These are possible answers:<br><ul>";
    foreach($question_array['answers'] as $answer) {
        echo "<li>" . $answer . "</li>";

You can do anything you want with those arrays in that loop, and it'll go through them. I've never stored a whole object like that, but it should totally be possible. Let me know how it goes.

  • can you give me a little tips on how the code work? or any resource you know to get me started with this, I'm thinking to create a repeatable <li> and then in the <li> will contain more than one field (question,answer option, correct answer) but don't know how to save / store this data in custom meta, I only know to store the data one for each it will be a lot of job if it's not loop able. – nonsensecreativity Jan 4 '13 at 19:11
  • I edited the answer to have more for you. – Dennis Smolek Jan 6 '13 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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