• 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);
                        ?>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m moving your topic to the Developing WP forum. As a beginner, you may not consider yourself a developer. No matter, because if you are writing your own code and have a question in that regard, it’s a topic for Developing WP.

    It’s not clear to me where the custom field comes into play. Where does this custom field reside? This field is used to manage whether this “sticky” post should be displayed first or not, correct? Your first post isn’t a true “sticky” in terms of how WP uses the term, but the concept is the same, so that’s what I’m calling it.

    Is this sticky the same post no matter the search terms? Or is it plucked out of search results by some criteria?

    Plucking a post out of query results so it is displayed first is kind of a pain. If you know what this post will be without query results, it’s easier to simply get the post separately and output it first. You can add an exclusion parameter to the query so there is no chance the post will get listed twice in the results. Of course this should all be done conditionally based on the value of the custom field.

Viewing 1 replies (of 1 total)
  • The topic ‘I want to place a post before all others from an ACF boleen field’ is closed to new replies.