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:
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“.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.