Categories
CMS Mastering Development Wordpress

Orderby is working with one query but not with other

I’m using a shortcode to show some price boxes. I use a CPT with a custom field called ‘posicion’ which has a number that I use later to customize the loop. This is the code:

<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// Add Shortcode
function mostrar_tarifas_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'tipo'      => 'fibra',
            'cantidad'  => '3',
            'boton'     => 'Lo quiero',
            'destacada' => '1',
        ),
        $atts,
        'mostrar_tarifas'
    ); 

    ob_start(); ?>
    <div class="grid-container center-grid">
        <?php
        $loop = new WP_Query( [
            'post_type' => 'tarifa',
            'tax_query' => [
                [
                    'taxonomy'  => 'tipo',
                    'terms'     => $atts['tipo'],
                    'field'     => 'slug'
                ]
            ],
            'meta_query'    => [
                [
                    'key'   => 'destacada',
                    'value' => $atts['destacada'],
                ]
            ],
            'posts_per_page' => $atts['cantidad'],
            'orderby'   => 'posicion',
            'order'     => 'ASC'
        ]
        );
        ?>

        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

        <!-- do stuff -->
        <div class="grid-25 tablet-grid-50 mobile-grid-100">
            <div class="wp-block-group bloque-precios">
                <div class="wp-block-group__inner-container">
                    <h3><?php the_title(); ?></h3>
                    <?php $precio = explode(".", carbon_get_the_post_meta('precio')); ?>
                    <div class="bloque_precios__price">
                        <span class="bloque_precios__integer-part"><?php echo $precio[0]; ?></span>

                        <div class="bloque_precios__after-price">
                            <span class="bloque_precios__fractional-part"><?php echo $precio[1]; ?></span>

                            <span class="bloque_precios__period elementor-typo-excluded"><?php echo carbon_get_the_post_meta('periodo'); ?></span> </div>

                        <span class="bloque_precios__currency elementor-currency--after">€</span>
                    </div>
                    <ul class="bloque-precios__features-list">
                        <li class="elemento-lista">
                            <div class="bloque-precios__feature-inner">
                                <span>
                                <?php echo carbon_get_the_post_meta('carac_1'); ?> </span>
                            </div>
                        </li>
                        <li class="elemento-lista">
                            <div class="bloque-precios__feature-inner">
                                <span>
                                <?php echo carbon_get_the_post_meta('carac_2'); ?> </span>
                            </div>
                        </li>
                        <li class="elemento-lista">
                            <div class="bloque-precios__feature-inner">
                                <span>
                                <?php echo carbon_get_the_post_meta('carac_3'); ?> </span>
                            </div>
                        </li>
                    </ul>
                    <div class="bloque-precios__footer">
                        <a class="bloque-precios__button button-precios button-size-lg" href="#"><?php echo $atts['boton']; ?></a>

                    </div>
                </div>
            </div>
        </div>

        <?php endwhile; wp_reset_query(); ?>
    </div>
    <?php

    return ob_get_clean();

}
add_shortcode( 'mostrar_tarifas', 'mostrar_tarifas_shortcode' );

And it’s working with a loop with the ‘fibra’ taxonomy, but not with others.

This image will help to understand my problem:

First group is the ona with ‘fibra-movil’ taxonomy, and the second one has ‘fibra’ taxonomy.

Leave a Reply

Your email address will not be published. Required fields are marked *