Order Results by Meta Value in WordPress

The WP_Query class is a powerful tool for retrieving data from WordPress. It can be used to query posts, pages, or custom post types, as well as filter results by date, category, or tag. The get_posts() function is a helper function that makes it easy to use the WP_Query class. It accepts an array of arguments, which can be used to query posts, pages, or custom post types.

One of the most useful arguments for both methods is ‘orderby’, which allows you to control how the results are ordered. By default, WordPress will order results by post date, but you can also order by title, author, ID, menu order, or any number of other values.

One common use case for ordering results by a meta value is when you have a custom field that stores a numeric value. For example, you may have a custom field that stores the price of a product, or the rating of a movie.

$args = [
	'post_type' 	   => 'post',
	'post_status'      => 'publish',
	'posts_per_page'   => 100,
	'offset'           => 0,
	'meta_key' => 'sub_header',
	'orderby' => 'meta_value',
	'order' => 'ASC',	
];

$posts = get_posts($args);
$query = new WP_Query($args);

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close