$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - I want to place a post before all others from an ACF boleen field|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - I want to place a post before all others from an ACF boleen field

matteradmin7PV0评论

I am currently trying to retrieve an post (to put it before all others in the search results of a search filter I created) from an ACF boolean field (and I am a beginner), so I made this script but I don't get the expected result:

in my function.php:

// Set Meta Query Relation
        $metaQueryRelation = array( 'relation' => 'AND' );

        // Build Meta Query Params
        $metaQueryParams = array(
            'relation' => 'OR',
            'display_not_first' => array(
                'key' => 'order_by_first_post',
                'compare' => 'NOT EXISTS'
            ),
            'display_first' => array(
                'key' => 'order_by_first_post',
                'compare' => 'EXISTS'
            ),
        );

In the result file

// Get WP_Query custom args.
$argsRecipesGrid = buildArgs($params);

$display_first = $metaQueryParams['display_first']['compare'];

$result[$key] = arrayCopy( $val );

$queryRecipesGrid = new WP_Query( $argsRecipesGrid );

/** partie de code Richardson **/

$queryRecipesGrid = array();

// The sorting loop
foreach ($queryRecipesGrid as $post ){
        if ($display_first['compare'] == 'EXISTS'){
            array_unshift($display_first, $post); 
        }
        if($post->have_posts()):
        ?>
        <div class="recipes-row<?php echo $params['mines'] ? ' row-count-3' : ''; ?>">
            <?php
                while( $queryRecipesGrid->have_posts() ): $queryRecipesGrid->the_post();    
            ?>
            <div class="recipe-col">

                <a class="list--item-link" <?php if(get_post_status() == 'publish'){?>href="<?php echo get_permalink(); ?>"<?php }; ?>>
                        <?php get_template_part('tpl/recipes/card'); ?>
                </a>

            </div>

            <?php
                endwhile;

                    echo '</div>';

                    else :

                    echo '<div class="grid-row">';

                    echo '<div class="grid-col-12">';

                    echo '<p>' . __('Aucun résultat', 'galbani') . '</p>';

                    echo '</div>';

                    echo '</div>';

                    endif; 
                    ?>

                    <?php
                    echo get_pagination($queryRecipesGrid, $params);
                    ?>

I am currently trying to retrieve an post (to put it before all others in the search results of a search filter I created) from an ACF boolean field (and I am a beginner), so I made this script but I don't get the expected result:

in my function.php:

// Set Meta Query Relation
        $metaQueryRelation = array( 'relation' => 'AND' );

        // Build Meta Query Params
        $metaQueryParams = array(
            'relation' => 'OR',
            'display_not_first' => array(
                'key' => 'order_by_first_post',
                'compare' => 'NOT EXISTS'
            ),
            'display_first' => array(
                'key' => 'order_by_first_post',
                'compare' => 'EXISTS'
            ),
        );

In the result file

// Get WP_Query custom args.
$argsRecipesGrid = buildArgs($params);

$display_first = $metaQueryParams['display_first']['compare'];

$result[$key] = arrayCopy( $val );

$queryRecipesGrid = new WP_Query( $argsRecipesGrid );

/** partie de code Richardson **/

$queryRecipesGrid = array();

// The sorting loop
foreach ($queryRecipesGrid as $post ){
        if ($display_first['compare'] == 'EXISTS'){
            array_unshift($display_first, $post); 
        }
        if($post->have_posts()):
        ?>
        <div class="recipes-row<?php echo $params['mines'] ? ' row-count-3' : ''; ?>">
            <?php
                while( $queryRecipesGrid->have_posts() ): $queryRecipesGrid->the_post();    
            ?>
            <div class="recipe-col">

                <a class="list--item-link" <?php if(get_post_status() == 'publish'){?>href="<?php echo get_permalink(); ?>"<?php }; ?>>
                        <?php get_template_part('tpl/recipes/card'); ?>
                </a>

            </div>

            <?php
                endwhile;

                    echo '</div>';

                    else :

                    echo '<div class="grid-row">';

                    echo '<div class="grid-col-12">';

                    echo '<p>' . __('Aucun résultat', 'galbani') . '</p>';

                    echo '</div>';

                    echo '</div>';

                    endif; 
                    ?>

                    <?php
                    echo get_pagination($queryRecipesGrid, $params);
                    ?>
Share Improve this question asked Feb 18, 2019 at 10:03 Frenchy_WPFrenchy_WP 191 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It looks like you're clearing the queried posts by setting the posts var to an empty array.

$queryRecipesGrid = new WP_Query( $argsRecipesGrid ); // first you get the posts here    
/** partie de code Richardson **/    
$queryRecipesGrid = array(); // then you wipe them out by setting the same var to empty array.

Regarding the sorting, please refer to the example I provided you earlier, How to always display a specific post from the search result first

$my_acf_value = 1; // it might make sense to have this as post ID (as integer)
// Do your query
$queryRecipesGrid = new WP_Query( $argsRecipesGrid );
// Sort posts
$sorted_posts = array();
if ($queryRecipesGrid->posts) {
    foreach ( $queryRecipesGrid->posts as $post ) {
        if ( $post->ID == $my_acf_value ) { // change this to match your needs
            array_unshift($sorted_posts, $post);
        } else {
            $sorted_posts[] = $post;
        }
    }
}
// Render the posts
if ($sorted_posts) {
    foreach($sorted_posts as $sorted_post) {
    // html stuff
    }
}

NB: I used post ID for sorting in this example for the sake of simplicity. Please modify the code to match your real use case and data.

Post a comment

comment list (0)

  1. No comments so far