Order ASC and DESC according to custom_field

4

I have a site session (WordPress) where I add partners according to the category using Custom Post Type. It's a party hall site. Each category of custom post type represents a branch. Ex of categories: Buffet, Photographers, Etc

So far so good. It's working Ok.

It has a field created as custom field named "wpcf-priority-partner" and I want it to work like this: If equals 0 it sorts partners in normal alphabetical order. If the value is nonzero (from 1 to 9) it has to sort these above the partners listed with 0 and in descending order of priority. 9 over 8, 8 over 7, and so on.

The intention is when a partner pays to stay in evidence he goes to the top of the list according to the priority (from 1 to 9) and those who are set to 0 are in alphabetical order below those that are a priority

Ps .: I'm a designer, I venture into ear PHP. But I have to ask for a HELP.

What I did is list the priorities correctly at the top of the list in descending order from 9 to 1, but those with priority 0 are listed alphabetically in descending order too (Z to A), but I want these are listed alphabetically from A to Z and below the priority.

How to make an 'order' = > 'ASC' sorting by title only for those with the 'wpcf-priority-partner' field listed as 0?

Here's what I did:

<?php

$args=array(
'post_type' => 'parceiros',
'posts_per_page' => 150,
'meta_key' => 'wpcf-prioridade-parceiro',
'orderby' => 'wpcf-prioridade-parceiro',
'order' => 'ASC');

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); 

?>
    
asked by anonymous 30.10.2014 / 05:12

1 answer

4

I think it's best to do two queries, one with priorities sorted from 9 to 1, and one with priorities 0 sorted alphabetically.

With the results you can use the WordPress plugin - Combine Queries plugin to merge the results and continue to from here:

Consult only priorities 1 to 9

/* Consulta apenas prioridades 1 a 9
 */
$args1 = array(
    'post_type'      => 'parceiros',
    'posts_per_page' => 150,
    'meta_key'       => 'wpcf-prioridade-parceiro',
    'meta_type'      => 'NUMERIC',
    'orderby'        => 'wpcf-prioridade-parceiro',
    'order'          => 'DESC',
    'meta_query'     => array(
        array(
            'key'     => 'wpcf-prioridade-parceiro',
            'value'   => array( 1,2,3,4,5,6,7,8,9 ),
            'compare' => 'IN',
        ),
    ),
);

Query only priority 0 sorted

/* Consulta apenas prioridade 0 ordenado
 */
$args2 = array(
    'post_type'      => 'parceiros',
    'posts_per_page' => 150,
    'orderby'        => 'title',
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'wpcf-prioridade-parceiro',
            'value'   => '0',
            'compare' => '=',
        ),
    ),
);

Merge queries

/* combinar as consultas
 */
$args = array(
    'posts_per_page' => 150,
    'paged'          => ( $paged = get_query_var( 'page' ) ) ? $paged : 1 ,
    'args'           => array( $args1, $args2 ),
);

Get results

if( class_exists( 'WP_Combine_Queries' ) ):
    $q = new WP_Combine_Queries( $args );
    if( $q->have_posts() ):
        ?><ul><?php
        while( $q->have_posts() ): $q->the_post();
        ?><li><a href="<?php the_permalink();?>"><?php the_title();?></a></li><?php
        endwhile;
        ?></ul><?php
        wp_reset_postdata();
    else:
        _e( 'Sorry no posts found!' );
    endif;       
endif;

Alternative

This may or may not work, it all depends on what you want to do.

Assuming you want to sort by priority from 9 to 0 and then get alphabetized from A to Z within each priority you can:

// ...
'orderby' => array(
    'wpcf-prioridade-parceiro' => 'DESC', // 1º de 9 a 0 na prioridade
    'title'                    => 'ASC'   // Depois de A a Z no título
)
// ...
    
30.10.2014 / 09:35