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);
?>
]]>
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.
]]>