A
A
AlpineMilk2018-04-14 23:41:46
WordPress
AlpineMilk, 2018-04-14 23:41:46

How to sort records using custom fields?

WP has an arbitrary field top_partner (true/false), I want all posts on the page to be sorted by this field, if true then these posts are on top, if false then posts are on the bottom.

$query = new WP_Query(array(
                           'post_type' => 'partner',
                           'posts_per_page' => -1,
                           'meta_key'=> 'top_partner',
                           'orderby' => 'meta_value',
                           'order' => 'DESC',
                           'tax_query' => array(
                                               array(
                                                   'taxonomy' => 'partner-city',
                                                   'field' => 'term_id',
                                                   'terms' => $category->term_id,
                                                   //'operator'=> 'NOT IN'
                                                     )
                                               )
                                         )
                                         );

                    $posts = $cat = array();
                    while ($query->have_posts()) {
                        $query->the_post();
                        //the_post();
                        $post = $query->post;                           
                        //$taxonomies = wp_get_post_categories($post -> ID); print_r($post); exit;
                        $categories = get_the_terms($post -> ID, 'partner-city');
                        if(count($categories) > 0){
                            foreach ($categories as $key => $value) {
                                if($value->parent==$category->term_id || $value->term_id==$category->term_id)
                                $posts[$value->name][] = $post;
                            }
                        }else                            
                        $posts[$category->name][] = $post;

                    }

but this way I only see entries in which top_partner is in true. What needs to be changed in order to display the rest of the records?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vorotnev, 2018-04-17
@HeadOnFire

1. Do not use ever posts_per_page' => -1, with a large database volume, it will come out sideways in terms of performance. A sufficient limit is always set , for example, if you will display a hundred pieces - set the limit to 100.
2. Do not use true/false values, use 1/0, and with this use meta_value_num - it will work faster and more predictably (see next paragraph).
3. It is possible that records with a value of false are cut out of the selection precisely because of this value (WP_Query is generally a funny constructor, especially with WP_Meta_Query / WP_Term_Query subqueries, and even more so with both at the same time).
4. If it doesn't work after changing to 0/1 and meta_value_num, it might be worth trying the posts_groupby filter .
PS: Now there is no way to check it for yourself, let me know in the comment if it doesn’t work out, I’ll pick it up later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question