• Hi,

    at the moment my theme I load posts with AJAX. But how can I can get also post meta keys via AJAX?

    At the moment functions PHP is like this:

    add_action('wp_ajax_nopriv_ajax_action', 'ajax_loading');
    add_action('wp_ajax_ajax_action', 'ajax_loading');
    
    function ajax_loading() {
    	switch($_REQUEST['fn']) {
    		case 'get_latest_posts':
    		$output = ajax_get_latest_posts($_REQUEST['count']);
    		break;
    		default:
    		$output = 'Error. No function specified.';
    		break;
    	}
    	$output = json_encode($output);
    	if (is_array($output)) {
    		print_r($output);
    	}
    	else {
    		echo $output;
    	}
    	die;
    }
    
    function ajax_get_latest_posts($count) {
         $posts = get_posts('numberposts='.$count.'&post_status=publish');
    
         return $posts;
    }

    And jQuery:

    $.ajax({
    		url: 'https://domain.com/wp-admin/admin-ajax.php',
    		type: 'POST',
    		data: {
    			'action': 'ajax_action',
    			'fn': 'get_latest_posts',
    			'count': 15
    		},
    		dataType: 'JSON',
    		success:function(data){
    			//print stuff here
    		},
    		error: function(errorThrown){
    			//error stuff here
    		}
    	});

  • The topic ‘Loading metakeys with AJAX’ is closed to new replies.