Order ASC and DESC according to custom_field

Asked

Viewed 479 times

4

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

So far so good. It’s working OK.

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

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

Ps.: I am a designer, I venture into orelhada PHP. But there are times I have to ask for a HELP.

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

How to make an order=> 'ASC' by 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(); 

?>

1 answer

4


I think it would be better to have two consultations, one with priorities ordered from 9 to 1, and the other with priorities 0 alphabetically ordered.

With the results you can make use of the plugin Wordpress plugin - Combine Queries to join the results and continue normally from there:

Consultation 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 priority only 0 ordered

/* 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' => '=',
        ),
    ),
);

Combine the consultations

/* 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 alternative may or may not work, it all depends on what you intend to do.

Assuming you want to sort by priority 9 to 0 and then alphabetize 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
)
// ...
  • 1

    It worked perfectly. I only had to customize the way the result was shown, because the partners shown with priority other than 0 need to be highlighted using a CSS class different from the others. Thank you so much for your help. Congratulations on your efficiency and patience in helping. I think it will be useful for many people, because I haven’t found any article about it. !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.