• pierrepierrepierre

    (@pierrepierrepierre)


    Hi all,
    I’m working on a website where I need to display also future posts (for an event custom post type).
    I managed to display it the way I want and make a nice calendar.

    But then, on the single view of each event (using single.php), the navigation links only directs to published event and not future ones.

    My former pre_get_posts function is

    function future_posts( $query ) {
    
    if( !is_admin() && $query->is_main_query() ) {
    		$query->set('post_status', array( 'publish','future' ));
    	}
    }
     add_action( 'pre_get_posts', 'future_posts' );

    I tried to add a line to change the query on single page :

    if (is_single() ) {
    		$query->set('post_status', array( 'publish','future' ));
    	}

    and i also tried to add the line without conditional tags “is_single()”, but it didn’t change anything.

    Thanks for your help!

    pierre

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If $query->set() without conditionals has no effect then something odd is going on. I suggest you hook ‘posts_request’ to examine the actual SQL query that is constructed. Something about how it’s formed should give you a clue to what the problem is. If all else fails you could directly alter the query through this filter or ‘posts_where’.

    Do everything possible to resolve ‘pre_get_posts’ before resorting to SQL modifications, they are a tactic of last resort.

    Thread Starter pierrepierrepierre

    (@pierrepierrepierre)

    Thanks bcworkz!

    unfortunately, the posts_request hook makes me able to display future post on single and archive pages, but they are still missing in the navigation links (posts_nav_link)…

    What i did, after reading the codex reference :

    I desactivate my custom must-use plugin that calls pre_get_posts and add following lines to the function.php, according to the code example

    function alter_the_query( $request ) {
        $dummy_query = new WP_Query();  // the query isn't run if we don't pass any query vars
        $dummy_query->parse_query( $request );
    
        // this is the actual manipulation; do whatever you need here
        if ( $dummy_query->is_home() )
            $request['post_status'] = array( 'publish','future' );
    
        return $request;
    }
    add_filter( 'request', 'alter_the_query' );

    Actually it works as my future posts are displayed (if I remove this i get a 404 error on refresh), but theses posts are still missing in the navigation links.

    It’s like the navigation links don’t use the main query…

    Thread Starter pierrepierrepierre

    (@pierrepierrepierre)

    Ok, after further research in the cascading wordpress function maze….
    It appear that the function we are talking about: the_post_navigation()
    uses
    > get_the_post_navigation()
    which uses
    > get_previous_post_link()
    which uses
    > get_adjacent_post_link()
    wich uses
    > get_adjacent_post
    where, finally, only the published posts are retained. Line 1575 :
    $where .= " AND ( p.post_status = 'publish'";
    details here : https://developer.www.ads-software.com/reference/functions/get_adjacent_post/

    So is there any way to alter this? or anybody would recommand to build myself some function to directly request theses adjacent posts links? With which method?

    Anyway, it intersting to see all the functions used by one function !

    Moderator bcworkz

    (@bcworkz)

    “cascading function maze” ?? yes indeed!

    As you have found, the navigation template tags are not too useful for custom applications because they create their own queries that are difficult or impossible to filter. Thus your only recourse is write your own equivalent functions that work for your application.

    While you can use core code as a guide, you can often discard large parts of it because you don’t require similar flexibility. OTOH, following the logic through the maze can be difficult. Sometimes it’s better to go it alone using your own wits and mySQL with $wpdb to get what you want. It basically comes down to keeping track of where the user is and using offset and limit to manage what comes next.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Navigation link and pre_get_post’ is closed to new replies.