admin管理员组

文章数量:1122846

I have this code:

<?php $most_popular_args = array(
            'post_type'     => 'post',
            'posts_per_page'  => 10,
            'orderby' => 'meta_value_num',
            'meta_key' => 'post_views_count',
            'order' => 'DESC'
            );
$the_query = new WP_Query( $most_popular_args ); ?>

            <?php if ( $the_query->have_posts() ) : ?>


            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php if ( has_post_thumbnail()) {
                $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
                $domain = get_site_url(); 
                $relative_url = str_replace( $domain, '', $medium_image_url[0] );
            }?>
            <a href="<?php the_permalink() ?>" class="flex news-small">
                <div class="flex flex-col pb-2 pt-1">
                    <h3 class=" text-[1.2rem] mb-2"><?php the_title() ?></h3>
                    <div class="mt-auto text-gray-600">
                        <p class="text-sm">Data: <?php echo get_the_date(); ?>
                        </p>
                        <p class="text-sm">Kategoria: <?php list_categories(get_the_category(),true) ?></p>
                        <?php var_dump(get_post_meta(get_the_ID(),'post_views_count', true ))?>
                    </div>
                </div>
            </a>
            <?php endwhile; endif?>

But it is not sorting my posts correctly. I have var_dump()ed the 'post_views_count' and see that it is string. Can this be the reason? But I think that 'orderby' => 'meta_value_num' treates this value as number? Is in it so?

So how to correctly sort the data. Now it displays in this order which is wrong: 8,6,31,7678,7415,6020

本文标签: wp queryorder metavaluenum not working