(已解决)WP_QUERY($args)->如何对同一数组中的字母和数字进行排序

时间:2018-08-31 作者:Gabriel Carneiro

我正在开发一个系统,在这里我有一个甲板演示
我有一个字段,该字段根据“meta\\u value”数组规范中的“copas”类型的卡进行排序,我希望它的排序与数组中的排序完全相同:

array(\'K\',\'Q\',\'J\',\'10\',\'9\',\'8\',\'7\',\'6\',\'5\',\'4\',\'3\',\'2\'),  
但下一个顺序是:

\'10\',\'9\',\'8\',\'7\',\'6\',\'5\',\'4\',\'3\',\'2\',\'J\',\'Q\',\'K\'
我不知道怎么做,你能帮我吗?

$args = array(
                        \'post_type\' => \'copas\',
                        \'posts_per_page\' => \'12\',
                        \'meta_key\' => \'dbt_carta\',
                        \'meta_value\' => array(\'K\',\'Q\',\'J\',\'10\',\'9\',\'8\',\'7\',\'6\',\'5\',\'4\',\'3\',\'2\'),
                        \'compare\' => \'LIKE\',
                        \'orderby\' => \'meta_value_num\',
                        \'order\' => \'DESC\'
                    );

                    // A Query
                    $cartas = new WP_Query( $args );
请记住,我需要它按此顺序排列,因为另一个过滤器将以$args的形式出现,因为这些卡将处于活动/非活动状态,并且它们必须始终按阵列的顺序排列,而不是按帖子的顺序排列。

正如@PatJ所说,它工作得很好,我非常感谢大家的帮助!Below the result

This is my meta-box-field:

array(
            //Texto referente ao campo | ponto (N)
            \'name\' => \'Carta\',

            //Texto de exemplo
            \'desc\' => \'Selecione a Carta\',

            //SEMPRE MUDE - Nome registrado no Banco de dados
            \'id\' => $prefix . \'carta\',

            //NÃO MUDE 
            \'type\' => \'select\',

            \'options\' => array(\'A\',\'K\',\'Q\',\'J\',\'10\',\'9\',\'8\',\'7\',\'6\',\'5\',\'4\',\'3\',\'2\')
        )
This is the part of the code where I look for the post of the letters and I present on the screen:

<ul id="wpbgallery" class="clearfix">

            <?php 
            $contador = 1 ;
            ?>
            <li class="carta-as" style="justify-content: center;">
                <!-- inicio do loop -->
                <?php 
                // Os argumentos
                $args_A = array(
                    \'post_type\' => \'copas\', 
                    \'posts_per_page\' => \'1\',
                    \'meta_key\' => \'dbt_carta\',
                    \'meta_value\' => \'A\'
                );

                // A Query
                $carta_as = new WP_Query( $args_A );

                // O Loop
                while ( $carta_as->have_posts() ) : $carta_as->the_post();

                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' ); 
                    ?>
                    <a href="<?php echo $image[0]; ?>" title="<?php the_title(); ?>" data-gallery="">
                        <?php the_post_thumbnail("baralho"); ?>
                    </a>
                <?php endwhile; ?>
                <!-- fim do loop -->
            </li>
            <br>
            <li class="cartas">
                <!-- inicio do loop -->
                <?php 
                $args = array(
                    \'post_type\' => \'copas\',
                    \'posts_per_page\' => \'12\',
                    \'meta_key\' => \'dbt_carta\',
                    \'meta_value\' => array(\'K\',\'Q\',\'J\',\'10\',\'9\',\'8\',\'7\',\'6\',\'5\',\'4\',\'3\',\'2\'),
                    \'compare\' => \'LIKE\'
                );

                // A Query
                $cartas = new WP_Query( $args );
                usort( $cartas->posts, \'sort_cartas\' );

                function sort_cartas( $a, $b ) {
                    $single = true;
                    $a_dbt_carta = get_post_meta( $a->ID, \'dbt_carta\', $single );
                    $b_dbt_carta = get_post_meta( $b->ID, \'dbt_carta\', $single );
                    // If they\'re the same, don\'t worry about sorting.
                    if ( $a_dbt_carta == $b_dbt_carta ) {
                        return 0;
                    }
                    // Converts \'K\', \'Q\', and \'J\' to numbers, for easier comparisons.
                    $face_card_numbers = array( \'K\' => 13, \'Q\' => 12, \'J\' => 11 );
                    if ( in_array( $a_dbt_carta, array_keys( $face_card_numbers ) ) ) {
                        $a_dbt_carta = $face_card_numbers[ $a_dbt_carta ];
                    }
                    if ( in_array( $b_dbt_carta, array_keys( $face_card_numbers ) ) ) {
                        $b_dbt_carta = $face_card_numbers[ $b_dbt_carta ];
                    }
                    if ( $a_dbt_carta > $b_dbt_carta ) {
                        return -1;
                    }
                    return 1;
                }


                if ($cartas->have_posts()) :  while ($cartas->have_posts()) :  $cartas->the_post(); 
                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' ); 
                    ?>
                    <a href="<?php echo $image[0]; ?>" title="<?php the_title(); ?>" data-gallery="">
                        <?php the_post_thumbnail("baralho"); ?>
                    </a>

                    <?php if($contador == 4): ?>

                    </li>
                    <li class="cartas">

                    <?php endif; $contador++;  ?>

                <?php endwhile; ?>
            <?php else : ?>

                <p class="text-center">Não existe conteúdo aqui!</p>

            <?php endif; ?>
        </ul>  
This is a representation of the result I\'m getting, exactly what I wanted (PS: I can not show the print of the original screen since each card has sensitive and restricted data):
enter image description here

2 个回复
最合适的回答,由SO网友:Pat J 整理而成

查看orderby 参数打开WP_Query, 我看不出有什么办法。你可以(正如PeterHVD在回答中所建议的那样)将你的名片转换成数字;但是如果你想保持K|Q|J 值,我建议使用usort() 回调:

// Assuming that $cartas->posts contains your posts
usort( $carta->posts, \'wpse313015_sort_cartas\' );

// ...

function wpse313015_sort_cartas( $a, $b ) {
    $single = true;
    $a_dbt_carta = get_post_meta( $a->ID, \'dbt_carta\', $single );
    $b_dbt_carta = get_post_meta( $b->ID, \'dbt_carta\', $single );
    // If they\'re the same, don\'t worry about sorting.
    if ( $a_dbt_carta == $b_dbt_carta ) {
        return 0;
    }
    // Converts \'K\', \'Q\', and \'J\' to numbers, for easier comparisons.
    $face_card_numbers = array( \'K\' => 13, \'Q\' => 12, \'J\' = 11 );
    if ( in_array( $a_dbt_carta, array_keys( $face_card_numbers ) ) ) {
        $a_dbt_carta = $face_card_numbers[ $a_dbt_carta ];
    }
    if ( in_array( $b_dbt_carta, array_keys( $face_card_numbers ) ) ) {
        $b_dbt_carta = $face_card_numbers[ $b_dbt_carta ];
    }
    if ( $a_dbt_carta > $b_dbt_carta ) {
        return -1;
    }
    return 1;
}
此代码未经测试。您可能需要对其进行更改以满足您的特定需要。

SO网友:Peter HvD

你误解了\'meta_value\'. 它用于告诉查询您要搜索的内容,而不是它应按的顺序。

我建议您在dbt_carta K为13,Q为12,J为11。这将是一个简单的数字排序。

结束