$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'); ?>filters - How to always display a specific post from the search result first|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)

filters - How to always display a specific post from the search result first

matteradmin9PV0评论

I created a search filter that displays posts randomly, and I wonder how can I always display a specific post item in the first position of the search results and display the rest of the items normally.

$taxonomies = array('recipe_type', 'product_category', 'recipe_category', 'recipe_event', 'recipe_diet');

foreach ($taxonomies as $taxonomy) {
    $taxonomies_taxonomy[$taxonomy] = get_taxonomy($taxonomy);
    $taxonomies_terms[$taxonomy] = get_terms(
        array(
        'taxonomy'      => $taxonomy,
        'hide_empty'    => false,
        'meta_key'      => 'filter_order',
        'orderby'       => 'meta_value_num',
        'order'         => 'ASC'
        )
    );
    $taxonomies_selected[$taxonomy] = (get_query_var($taxonomy)) ? get_query_var($taxonomy) : '';
}

I created a search filter that displays posts randomly, and I wonder how can I always display a specific post item in the first position of the search results and display the rest of the items normally.

$taxonomies = array('recipe_type', 'product_category', 'recipe_category', 'recipe_event', 'recipe_diet');

foreach ($taxonomies as $taxonomy) {
    $taxonomies_taxonomy[$taxonomy] = get_taxonomy($taxonomy);
    $taxonomies_terms[$taxonomy] = get_terms(
        array(
        'taxonomy'      => $taxonomy,
        'hide_empty'    => false,
        'meta_key'      => 'filter_order',
        'orderby'       => 'meta_value_num',
        'order'         => 'ASC'
        )
    );
    $taxonomies_selected[$taxonomy] = (get_query_var($taxonomy)) ? get_query_var($taxonomy) : '';
}
Share Improve this question edited Feb 15, 2019 at 13:11 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Feb 15, 2019 at 13:08 Frenchy_WPFrenchy_WP 191 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

One way to show a specific post always first is to add a sorting loop between your post query and rendering loop.

For example,

$my_posts = array(); // do your query first to get the posts

$sorted_posts = array(); // helper var to store sorted posts

// The sorting loop
foreach ( $my_posts as $post ) {
    if ( "some logic here" ) {
        array_unshift($sorted_posts, $post); // use php function to prepend the post to the sorted array
    } else {
        $sorted_posts[] = $post; // just push the posts to the end of the sorted array
    }
}

// use $sorted_posts array in your rendering (foreach/while/for) loop
Post a comment

comment list (0)

  1. No comments so far