Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Cannot order numeric values with WP_Query

I am trying to sort my custom post type with multiple meta values, but since wordpress saves all metadata as longtext in the database, numeric order is not working.

Some information:

  • "cota" - the custom post type
  • "valor" - the numeric metadata field. Needs to be ordered DESC ( 9 - 0)
  • "administradora" - the text metadata field. Needs to be ordered ASC ( A-Z)

First, i group all posts by "administradora", and then by "valor". The problem is that i am not being possible of sorting in a "numeric way".

Example:

If i have the following cotas with values:

1000 200

Wordpress is ordering:

200 1000

Because it compares the value as text ( 1 comes before 2).

My code:

      $query = new WP_Query( array(
          'post_type' => 'cota',
          'posts_per_page' => -1,
          'tax_query' => array(
              array(
                  'taxonomy' => 'tipo',
                  'field'    => 'slug',
                  'terms'    => $atts['tipo'],
              ),
          ),
          // 'orderby' => 'meta_value_num',
          'meta_query' => array(
            'relation' => 'AND',
            'credito_clause' => array(
              'key' => "valor",
              'orderby' => 'meta_value_num',
              'type' => 'DECIMAL',
              'compare' => 'EXISTS'
            ),
            'adm_clause' => array(
              'key' => 'administradora',
              'compare' => 'EXISTS'
            )
          ),
          'orderby' => array(
            'adm_clause' => 'ASC',
            'credito_clause' => 'DESC'
          )
        )
      );

Thanks a lot for the help!

1 Answer

Solved.

      $query = new WP_Query( array(
          'post_type' => 'cota',
          'posts_per_page' => -1,
          'tax_query' => array(
              array(
                  'taxonomy' => 'tipo',
                  'field'    => 'slug',
                  'terms'    => $atts['tipo'],
              ),
          ),
          'meta_query' => array(
            'relation' => 'AND',
            'credito_clause' => array(
              'key' => "valor",
              'orderby' => 'meta_value_num',
              'type' => 'DECIMAL',
              'compare' => 'EXISTS'
            ),
            'adm_clause' => array(
              'key' => 'administradora',
              'compare' => 'EXISTS'
            )
          ),
          'orderby' => array(
            'adm_clause' => 'ASC',
            'credito_clause' => 'DESC'
          )
        )
      );