Post navigation in custom post type order by date

I know there is a ‘how to’ explanation in this forum for post navigation order bij Post Title in alphabetical order. I tried to adjust that for my situation, but it doesn’t work and I don’t know how to make it work (I’m no coder). So please, is there anybody who wants to help me and correct it for me? This is what I made of the code timmse provided in answer to the post before.

function filter_next_post_sort($sort) {
global $post;
if (get_post_type($post) == ‘Project’) {
$sort = “ORDER BY p.post_date ASC LIMIT 1”;
return $sort;
}
}
function filter_next_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == ‘Project’) {
return $wpdb->prepare(“WHERE p.post_date > ‘%s’ AND p.post_type = '”. get_post_type($post).“’ AND p.post_status = ‘publish’”,$post->post_date);
}
}

function filter_previous_post_sort($sort) {
global $post;
if (get_post_type($post) == ‘Project’) {
$sort = “ORDER BY p.post_date DESC LIMIT 1”;
return $sort;
}
}
function filter_previous_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == ‘Project’) {
return $wpdb->prepare(“WHERE p.post_date < ‘%s’ AND p.post_type = '”. get_post_type($post).“’ AND p.post_status = ‘publish’”,$post->post_date);
}
}

add_filter(‘get_next_post_sort’, ‘filter_next_post_sort’);
add_filter(‘get_next_post_where’, ‘filter_next_post_where’);

add_filter(‘get_previous_post_sort’, ‘filter_previous_post_sort’);
add_filter(‘get_previous_post_where’, ‘filter_previous_post_where’);

Hi

Just a quick thought.

Are you sure that the post type in the if statement has to be with a capital letter?

if (get_post_type($post) == ‘Project’)

Can you try with project instead of Project?

Also, is this in a loop?

Can you try this, if it does not work

if ( get_post_type( get_the_ID() ) == ‘project’ )

Cheers

Patric

Adjusted it, but now am getting a syntax error on ‘BY’. Yesterday that was no issue and could update the Child Theme functions.php. Weird.

Hi

I ran your code through WPcodebox.

your " and ’ signs are wrong… I think you copy / pasted the code and somehow the system took the wrong " and ’ sign.

code

You have to use the straight one " ’ and not the other one. I don’t know how to explain it…

e.g.

$sort = "ORDER BY p.post_date ASC LIMIT 1";

you have to replace all of them in your code.

Cheers

Patric

Try to copy / paste this version, where I have corrected all the wrong signs:

<?php 
function filter_next_post_sort($sort) {
global $post;
if (get_post_type($post) == 'Project') {
$sort = "ORDER BY p.post_date ASC LIMIT 1";
return $sort;
}
}
function filter_next_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == 'Project') {
return $wpdb->prepare("WHERE p.post_date > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_date);
}
}

function filter_previous_post_sort($sort) {
global $post;
if (get_post_type($post) == 'Project') {
$sort = "ORDER BY p.post_date DESC LIMIT 1";
return $sort;
}
}
function filter_previous_post_where($where) {
global $post, $wpdb;
if (get_post_type($post) == 'Project') {
return $wpdb->prepare("WHERE p.post_date < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_date);
}
}

add_filter('get_next_post_sort', 'filter_next_post_sort');
add_filter('get_next_post_where', 'filter_next_post_where');

add_filter('get_previous_post_sort', 'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');

In general, that’s why a code tool like WPcodebox comes in handy because it shows these kind of errors.

Cheers

Patric

Thank you Patric. It is working. :smiley: And indeed also the lower case ‘project’.

1 Like