Get custom posts based on meta field content

To get posts on basis of their meta content, You can use get_posts function as given below:

$args = array(
		'meta_key' => 'student_name', // Put Key name
		'meta_value' => 'Mark',  // Put key value
		'post_type' => 'students',  // Post type
		'post_status' => 'publish', // Get all published posts. 
		'posts_per_page' => -1  // Here -1 means you will get all post. You can add any positive number
	);

$posts = get_posts($args);

 

Another equivalent query using meta_query instead of meta_key and meta_value:

$args = array(
	'meta_query' => array(
		array(
			'key' => 'student_name',
			'value' => 'Mark'
		)
	),
	'post_type' => 'students',
	'posts_per_page' => -1
);
$posts = get_posts($args);

 

By Ashutosh Pratap

I am software developer by profession and doing work and research in field of Computer programmings like PHP, WordPress, Magento, jQuery, Google APIs and many more web languages. Apart from this I write blogs on programming languages like PHP, Javascript, WordPress etc. I am also individual and independent Freelancer so you can hire me for your web work.

Leave a comment

Your email address will not be published. Required fields are marked *