Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to sort recent WordPress posts using custom order based on anything

Posted on February 26, 2021March 5, 2021 by Habibie

I was trying to figure out how to make custom order of posts lists in our WordPress website.

It’s confusing at first, because what I need to do is to sort my posts ordered by custom meta box field of my posts.

In other words, I want to sort my posts by custom meta key / custom field of my posts (actually by meta value I mean).

For example I have a custom fields with keyname “poster_number”. So I did this code and place it on functions.php file of my theme:

//Custom Order
function order_posts_by_metavalue( $query ) { 
    if(is_category()) {
		$query->set( 'order' , 'ASC' );
		$query->set( 'orderby', 'meta_value');
		$query->set( 'meta_key', 'your_meta_key_here');
		return;
	}
} 
add_action( 'pre_get_posts', 'order_posts_by_metavalue' );

Hope that works for you too.

If you want to sort them by title, replace ‘poster_number’ with ‘title’.

Post Views: 249
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme