WordPress.org

Ready to get started?Download WordPress

Forums

pre_get_posts to display CPT on page template? (1 post)

  1. robmaurizi
    Member
    Posted 9 months ago #

    Hey folks- I've got several CPTs set up (news, video, in addition to posts), and I need to effectively create an index/archive style listing for each.

    I'm using a static page for the homepage, and I don't necessarily want to declare a single page as the "posts" page since I need several versions of that.

    Trying to do this with pre_get_posts by checking to see if we're looking for one of the pages that should drive this, and then changing the query thusly:

    function test_pgp($query) {
    	if ((!is_admin()) && ($query->is_main_query())) {
    		if (is_page('videos')) {
    			$query->set('post_type', 'husa_video');
    		}
    		if (is_page('news')) {
    			$query->set('post_type', 'husa_pr');
    		}
    		if (is_page('blog')) {
    			$query->set('post_type', 'post');
    		}
    	}
    }
    
    add_action('pre_get_posts', 'test_pgp');

    The is_page conditionals are returning true as desired, so that's not it, and I can even print_r the $query object inside those conditionals to make sure the post_type is getting set properly. It LOOKS right, but I'm not certain it actually is.

    Spitting out the $query object doesn't list any of the posts I'm looking for, and the front-end gets the 404 template.

    I've tried wiping out the page_id, I've tried setting is_page=false and is_archive=true, and I'm still not seeing any major changes.

    The above code is included by functions.php

    Thanks in advance for any pointers or direction you may all be able to provide!

Reply

You must log in to post.

About this Topic