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

admin2025-06-03  3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748894707a314612.html

最新回复(0)