Support » Developing with WordPress » Iterate over get_post_meta() result

  • I have WordPress code like below.

    function get_preselect_values()
    {
        $volunteers = get_post_meta($_POST['element_id'], "volunteers", false);
        $users = array();
    	
    	foreach ($volunteers as $volunteer) {
    		foreach ($volunteer as $volun) {
    			$users = $volun['ID'];
    		}
    	}
    	echo json_encode($users);
        die;
    }
    
    add_action('wp_ajax_get_preselect_values', 'get_preselect_values');
    add_action('wp_ajax_nopriv_get_preselect_values', 'get_preselect_values');

    I am getting only the First value.

    How can I get all values ?

    • This topic was modified 2 weeks, 6 days ago by mabufoysal.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not clear what sort of array structure you are dealing with. To gain more insight, var_dump($volunteers);

    The var_dump output may not be compatible with your Ajax call. Instead, call get_preselect_values() directly from template code. (After setting $_POST[‘element_id’] to a valid ID value.) Place the output within <pre> tags or else the browser will run all the output together.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.