Message sorting and custom fields

Quite often, WordPress developers are faced with the task of sorting posts by non-standard parameters according to user custom fields.. If you want to write a SQL query, it will be quite difficult, since you have to work with two database tables (wp_posts и wp_postmeta).

But there is a much easier way. In our case, we use the function query_posts. Below you can see an example, which includes custom fields in the request, and also sort the records with this custom custom field. Shortly speaking, easy to understand code with efficient result:

пользовательские произвольные поля WordPress и построение запросов и сортировки

Data fetch request:

query_posts(‘meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC’);

This method has one disadvantage., as far as I understand, you can work with only one custom field. Sometimes when filtering, it’s enough to just make a few categories and filter by them. Read more in the article “Combined WordPress Filtering Options“.


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal