• Resolved amurado

    (@amurado)


    Hi,

    I’m currently using the following code in my functions.php file to display scheduled posts for the custom post type “movies”.

    //DISPLAY FUTURE POST TYPES
    add_filter( 'pre_get_posts', 'get_future_posts' );
    function get_future_posts( $query ) {
    	if ( is_home() && $query->is_main_query() )
    		//IF YOU WANT TO INCLUDE CUSTOM POST TYPES
    		$query->set( 'post_type', array( 'post', 'movies' ) );
    		//INCLUDE POSTS WHOSE STATUS IS BOTH PUBLISHED AND FUTURE
    		$query->set( 'post_status', array( 'publish', 'future' ) );
    	return $query;
    }
    // SHOW FUTURE POSTS SINGLE.PHP
    function show_future_posts($posts){
    	global $wp_query, $wpdb;
    	if(is_single() && $wp_query->post_count == 0){
    		$posts = $wpdb->get_results($wp_query->request);
    	}
    	return $posts;
    }
    add_filter('the_posts', 'show_future_posts');

    However, while this code works great for what I want it to do, I noticed it for some reason affects my media library. Whenever I go to my media library or try and insert an image through media library it always tells me “No media files found.” even though I know there is. When I remove the code above, it shows me all the images like normal.

    Any ideas on how this is being affected and what I can do?

    Thank you

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

    (@bcworkz)

    You just need to include the set post_status line within the if conditional by adding curly braces.

    if ( is_home() && $query->is_main_query() ) {
    	$query->set( 'post_type', array( 'post', 'movies' ) );
    	$query->set( 'post_status', array( 'publish', 'future' ) );
    }

    What you have now is the conditional if only applies to the set post_type line. The set post_status line is applied to all queries. Because attachments do not use publish or future statuses (they use ‘inherit’), any queries for attachments with publish status, as in media queries, results in nothing found.

    BTW, while add_filter() works here, ‘pre_get_posts’ is actually an action, so you should really use add_action(). As an action, you do not need to return a value, or use return; at all. It doesn’t hurt to do so, but it is ignored. Changes to $query work without returning a value because your callback is passed the $query object by reference, so any changes are applied directly to the actual object in memory.

    Thread Starter amurado

    (@amurado)

    Hi bcworkz,

    Thanks for you reply! I just tried adding the curly braces as you mentioned, and while the media library works again, it no longer shows my future movies on the frontend. Have I done something wrong?

    Unfortunately, I don’t know much about this and your last paragraph does slightly confuse me. Are you able to look at my updated code and let me know what I need to do?

    Thanks for your help so far!

    //DISPLAY FUTURE POST TYPES
    add_filter( 'pre_get_posts', 'get_future_posts' );
    function get_future_posts( $query ) {
    	if ( is_home() && $query->is_main_query() ) {
    	$query->set( 'post_type', array( 'movies' ) );
    	$query->set( 'post_status', array( 'publish', 'future' ) );
    }
    	return $query;
    }
    // SHOW FUTURE POSTS SINGLE.PHP
    function show_future_posts($posts){
    	global $wp_query, $wpdb;
    	if(is_single() && $wp_query->post_count == 0){
    		$posts = $wpdb->get_results($wp_query->request);
    	}
    	return $posts;
    }
    add_filter('the_posts', 'show_future_posts');
    Moderator bcworkz

    (@bcworkz)

    While what you did is what I mainly intended (disregarding the last paragraph stuff), I misconstrued what the original code was doing. Let’s try a different approach, use this instead for the first callback:

    function get_future_posts( $query ) {
        if ( is_home() && $query->is_main_query() )
            $query->set( 'post_type', array( 'movies' ) );
        if ( 'attachment' != $query->get('post_type') )
            $query->set( 'post_status', array( 'publish', 'future' ) );
        return $query;
    }

    You understanding that last paragraph is not that important. What I was trying to explain is that, despite the fact that what you had worked (disregard the media library issue for now), it’s slightly incorrect. Instead of trying to explain why, I offer a “more correct” version:

    add_action('pre_get_posts', 'get_future_posts');
    function get_future_posts( $query ) {
        if ( is_home() && $query->is_main_query() )
            $query->set( 'post_type', array( 'movies' ) );
        if ( 'attachment' != $query->get('post_type') )
            $query->set( 'post_status', array( 'publish', 'future' ) );
    }

    If you re-read that last paragraph and compare your version with my version, maybe it’ll make more sense. If not, don’t worry, it’s not a big deal. Especially the pass by reference bit, that’s a rather advanced coding concept that’s hard to grasp at first.

    If you’re interested in expanding your coding knowledge, the difference between pass by reference and pass by value is a good thing to understand. You could try searching for explanations of the two concepts as a learning exercise. Or not ??

    Thread Starter amurado

    (@amurado)

    Thank you very much, that code did the trick!

    Thanks for also being very nice. Some people can be quite rude when clearly people are still trying to learn the ins and outs of WordPress. Will definitely continue to read and learn more.

    Thanks again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Code to Display Future Posts is Affecting My Media Library’ is closed to new replies.