Wp-query Order By problem

时间:2021-11-24 作者:Marcel CL

我试图根据他们写入查询的顺序对查询进行排序,但它需要按ID排序。

可变的$idpaginas打印:20659206262058520618464197481999118520205882126818753205621277188291855121132

但查询后,它会打印:1852018520185511875318829。。。。。

function LoopMode($atts, $output = null, $my_query = null, $query_args = null) {
    // Params extraction
    extract(shortcode_atts(array(
        \'idpaginas\'   => \'\',
        \'style\'       => \'\',
    ), $atts));

    $query_args = array(
        \'post_status\'    => \'publish\',
        \'post__in\'       => explode( ",", $idpaginas ),
        \'posts_per_page\' => -1,
        \'post_type\'      => \'page\',
        \'order_by\'       => \'post__in\',
        \'order\'          => \'ASC\',
    );
    if(empty($style)){ $style = \'2\'; }
    // Run query
    $my_query = new WP_Query( $query_args );
    //$output = \'\';
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <div class="cluster-box col-<?php echo $style; ?>">
            <a href="<?php the_permalink() ?>" class="cluster-mcl-link">
                <div class="mcl-cluster">
                    <div class="mcl-cluster-img" style="background-image:url(<?php the_post_thumbnail_url(); ?>);">
                        <div class="mcl-cluster-title"><h3><?php the_title(); ?></h3></div>
                    </div>
                </div>
            </a>
        </div>
    <?php endwhile;
    $my_query = null; $my_query = $temp;
    $output = ob_get_contents();
    ob_end_clean();
    wp_reset_postdata();
    return $output;
}
add_shortcode(\'cluster\', \'LoopMode\');
我已尝试删除args“;“订购人”;和;订单“;价值观,但它仍然按照他的意愿继续订购。

谢谢

1 个回复
SO网友:Buttered_Toast

“The”;“问题”;似乎是order接收的默认值,除非另有说明,否则其始终为DESC。

你需要在收到帖子后创建自己的订单,比如这样。

$my_query = new WP_Query($query_args);

$ordered_posts = [];

foreach(explode(\',\', $idpaginas) as $rpid) {
    foreach($my_query->posts as $index => $fpid) {
        if($fpid->ID == $rpid) $ordered_posts[] = $my_query->posts[$index];
    }
}

$my_query->posts = $ordered_posts;

while ($my_query->have_posts()) : $my_query->the_post();
此代码基于this 问题,对其进行了一些编辑以满足您的需要。

相关推荐

Show form per shortcode

这是我的密码。我想按短代码显示它。$user_id = get_current_user_id(); $balance = mycred_get_users_balance( $user_id ); echo "Current Balance: ".$balance."<br><br>"; if ($_POST && $_SERVER["REQUEST_METHOD"] == &qu