Sort PHP Array

1
//Argumentos para hacer la Query.
$args = array(
'post_type' => 'sectores',
'post_status '=> 'publish',
'posts_per_page' => -1
);

//Obtenemos las clinicas del sistema.
$sectores = new WP_Query($args);

//Si tenemos clinicas disponibles.
if ( $sectores->have_posts() ) 
{
//Creamos pila de clinicas.
$infoSector = array();       


  //Entramos en bucle para obtener las clinicas.
while ( $sectores->have_posts() ) : $sectores->the_post();
    if(get_the_post_thumbnail_url()){
        $location = array(
            "nombre" => get_the_title(),
            "slug" => get_post_field( 'post_name', get_post()),
            "imagen" => get_the_post_thumbnail_url(),
            "main" => get_field("main"),
            "custom_link" => get_field("custom_link"),
            "category" => get_the_terms( $post->ID, 'categoria_sector' ),
            "descripcion" => get_field("descripcion"),
            "orden" => get_field("orden")
        );
    } else {
        $location = array(
            "nombre" => get_the_title(),
            "slug" => get_post_field( 'post_name', get_post()),
            "imagen" => "http://www.yupcharge.com/wp-content/uploads/2018/08/sectores-yupcharge.jpg",
            "main" => get_field("main"),
            "custom_link" => get_field("custom_link"),
            "category" => get_the_terms( $post->ID, 'categoria_sector' ),
            "descripcion" => get_field("descripcion"),
            "orden" => get_field("orden")
        );
    }

    //Almacenamos las posiciones.
    array_push($programasEnvio, $location);

I have these data that I take with custom fields.

$sectoresPrint = array();
    foreach ($programasEnvio as $sector ) {
        if($sector['category'][0]->name == "Sector principal") {
            array_push($sectoresPrint, $sector);
        }
    }

How can I order the array sectorsPrint by the order field from least to greatest

    
asked by Caldeiro 09.10.2018 в 09:31
source

1 answer

1

Keeping in mind that $sectores is a WP_Query object created like this:

$sectores = new WP_Query($args);

You can use the parameters orderby and order that can receive objects WP_Query .

If you modify the array that you pass as an argument indicating the order, the data will come already ordered and you will not need to undertake a new manipulation on them to do something that you can do at the moment of obtaining them.

You can then pass the arguments like this:

$args = array(
'post_type'      => 'sectores',
'post_status'    => 'publish',
'posts_per_page' => -1,
'orderby'        => 'orden',
'order'          => 'ASC'
);
    
answered by 10.10.2018 / 13:21
source