# How to order posts by custom field when using Post Navigation?

**URL:** <https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970>\
**Category:** Uncategorized\
**Created:** [February 15, 2024, 11:45pm UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970 "2024-02-15T23:45:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloverbox](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cloverbox](https://forum.bricksbuilder.io/u/cloverbox)\
**Post date:** [February 15, 2024, 11:45pm UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970/1 "2024-02-15T23:45:26Z")

</div>

Hi!

As default, the Post Navigation element uses post ID for ordering.

Is it possible to use a custom field (number or text) to set a custom order and use that one for Post Navigation?

For example:  
I have 3 posts with post id 1, 2, 3 and they have custom field values of 18, 19, 17 respectively.  
By default, Post Navigation will lead me through 1 → 2 → 3.  
But I want to order them by the custom field, so I would have 3 → 1 → 2

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![ppt](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/p/f05b48/32.png) [@ppt](https://forum.bricksbuilder.io/u/ppt)\
**Post date:** [March 7, 2024, 5:02am UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970/2 "2024-03-07T05:02:58Z")

</div>

I found this and it works for me: [WordPress: sort Previous and Next post by menu\_order · GitHub](https://gist.github.com/amielucha/7a6f690ab14b67212049f287df686e67)

---

<div class="post-metadata">

**Author:** ![matutino](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/matutino/32/26858_2.png) [@matutino](https://forum.bricksbuilder.io/u/matutino)\
**Post date:** [June 13, 2025, 9:58pm UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970/3 "2025-06-13T21:58:28Z")

</div>

In case someone comes across this post and, like me, don’t get to solve the issue with the previous answer: I could fix the posts nav order by adding the following PHP code to functions.php file.  
Change “event\_date” for your meta key. Then instead of the posts navigation element add a shortcode in your page. In this case it’s [event\_date\_navigation], but you can change it on the last line of the code.

```auto

function custom_post_navigation_by_event_date() {
    global $post;

    if (!$post) return '';

    $meta_key = 'event_date';
    $current_event_date = get_post_meta($post->ID, $meta_key, true);

    $next = new WP_Query([
        'post_type' => get_post_type($post),
        'posts_per_page' => 1,
        'meta_key' => $meta_key,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => [
            [
                'key' => $meta_key,
                'value' => $current_event_date,
                'compare' => '>',
            ]
        ]
    ]);

    $prev = new WP_Query([
        'post_type' => get_post_type($post),
        'posts_per_page' => 1,
        'meta_key' => $meta_key,
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => [
            [
                'key' => $meta_key,
                'value' => $current_event_date,
                'compare' => '<',
            ]
        ]
    ]);

    ob_start();
    echo '<nav class="custom-event-post-navigation">';

    if ($prev->have_posts()) {
        $prev->the_post();
        echo '<div class="nav-previous"><span class="label">← Vorherige Veranstaltung</span><a href="' . get_permalink() . '">' . get_the_title() . '</a></div>';
    }

    wp_reset_postdata();

    if ($next->have_posts()) {
        $next->the_post();
        echo '<div class="nav-next"><span class="label">Nächste Veranstaltung →</span><a href="' . get_permalink() . '">' . get_the_title() . '</a></div>';
    }

    wp_reset_postdata();

    echo '</nav>';

    return ob_get_clean();
}
add_shortcode('event_date_navigation', 'custom_post_navigation_by_event_date');

```

---

<div class="post-metadata">

**Author:** ![PB86nl](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@PB86nl](https://forum.bricksbuilder.io/u/PB86nl)\
**Post date:** [January 26, 2026, 10:49am UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970/4 "2026-01-26T10:49:18Z")

</div>

Thank you for your reply. I hope you can help me one step further.

My post type = CPT “persons”

My numeric value = ACF numeric field “acf\_cm\_acf\_persoon\_nr\_op\_kieslijst”

I’m always cautious when it comes to PHP, because I don’t want to break my site.

Could you give me a head start on this? Would be greatly appreciated!

Kind regards,

Peter

---

<div class="post-metadata">

**Author:** ![glp](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/glp/32/18580_2.png) [@glp](https://forum.bricksbuilder.io/u/glp)\
**Post date:** [August 1, 2026, 6:16pm UTC](https://forum.bricksbuilder.io/t/how-to-order-posts-by-custom-field-when-using-post-navigation/19970/5 "2026-08-01T18:16:46Z")

</div>

Another option is to use a plugin like [Post Types Order](https://wordpress.org/plugins/post-types-order/). I am using this successfully to order posts for query loops and the previous/next links. One note is that you have to swap the labels and icons for the links.
