How do you query a post type created in custompress?

I’ve installed custompress and have created 4 different post types. I would like to query each on a separate template.php page – however everything I’ve tried doesn’t pull that specific post type – instead all of my post types are appearing on the template page.

What is the best way to do this? Please note that I’m referring to post types and NOT taxonomy – have no need to create any taxonomies for these posts.

Things I’ve tried but didn’t work….

query_posts( array(‘post_type’ => array( ‘name of post type’ ) )

I’m also assuming that because this is a plugin, I should NOT have to update functions.php with any of my post_types.

  • Ivan
    • The Incredible Code Injector

    Hi strategicblend,

    Here is how you can do that:

    1. I see you have added the 4 post_types already but make sure that the settings for each one are set to public.

    2. Make 4 different pages in your admin panel ( one for each post_type ) and create four different page templates ( one template for each page ) http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates . These templates will be used for outputting the custom post types. Assign each page one of the templates from the attributes meta-box.

    3. Use query_posts for outputting the custom post types ( make sure you have added some posts to each new post type ) on each page.

    Each of your templates should query a different post type and it should look something like that:

    //The Query
    query_posts( array( 'post_type' => 'my_post_type', 'post_status' => 'publish' ) );

    //The Loop Start
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ..
    endwhile; else:
    ..
    endif;
    //The Loop End

    //Reset Query
    wp_reset_query();

  • strategicblend
    • Design Lord, Child of Thor

    Hey Ivan – its step 3 that isn’t working. I’ve tried about every query method I could get my hands on (including the one you mentioned above) and none of them pull just a single custom post type – I keep getting everything displayed on the page.

  • Ivan
    • The Incredible Code Injector

    I don’t see the wp_reset_query() anywhere. This is very important, you have to always call wp_reset_query() after any query_posts() call. Make sure you have this everywhere where you call query_posts() in the format outlined in my initial post.

    Also I see you are using an uppercase letter in the post_type system name, try with lowercase letter or try to add a post type which has its system name in lowercase letters.

  • strategicblend
    • Design Lord, Child of Thor

    I have been able to do any number of successful queries in the past without using reset. I did add it per your suggestion above and still have the same issue – ALL post_types are being called not just the single post_type. I also tried lowercasing and tried calling other post types as single display – nothing works.

    There might be a bug in how the plugin executes post_types – when I set this up via hardcoding on my functions.php page, I don’t have any issues. I’d rather use the plugin though.

  • Ivan
    • The Incredible Code Injector

    Check whether the “Display post types on “Home”:” CustomPress option filters a particular post_type on front. Make sure you do the test with a post_type with a system name with all lowercase characters without spaces, dashes etc. Also make sure the post_type is set to public.

  • strategicblend
    • Design Lord, Child of Thor

    All post_types are set to public and I have already tested lowercase letters, etc (mentioned above). The “Events” post_type is currently NOT checked to display on home. I don’t want it appearing there though, so that shouldn’t be an issue.

  • Ivan
    • The Incredible Code Injector

    Hi strategicblend,

    All post_types are set to public and I have already tested lowercase letters, etc (mentioned above). The “Events” post_type is currently NOT checked to display on home. I don’t want it appearing there though, so that shouldn’t be an issue.

    No, I mean I want you to try to filter a particular post type on front from the Display post types on “Home” so if this works then you can copy the query_posts code from there.

  • Ivan
    • The Incredible Code Injector

    The function responsible for setting the post_types is located at Line 140 from custompress/core/core.php .

    So basically what it does is to filter the query with:

    Call the filter:

    add_filter( 'pre_get_posts', array( &$this, 'display_custom_post_types' ) );

    So the function display_custom_post_types( $query ) accepts the query parameter which then is filtered.

    $query->set( 'post_type', 'myposttype' );
    return $query

    In your templates you can use global $wp_query instead of the filter hook.

    Also you may try this approach:

    global $query_string;
    query_posts( $query_string . "&post_type=classifieds&post_status=publish");

    Additionally, what’s the purpose of the “templates.” Those don’t really work either…..when I download the updated “single-templatepage.php” no query has been added.

    This copies the single.php template into single-[post_type].php template which will be used when displaying post with the particular post type. You may want to output other post types with this template as well so the query_posts() is not set by default.

  • strategicblend
    • Design Lord, Child of Thor

    When I try

    <?php $wp_query ( 'pre_get_posts', array( &$this, 'display_custom_post_types' ) );
    $query->set( 'post_type', 'blog_posts' ); return $query ?>

    I get the following error message:

    Fatal error: Function name must be a string in /homepages/33/d311183085/htdocs/rohreringsuccess/wp-content/themes/thick/template-events.php on line 12

    And when I try

    <?php global $query_string;
    query_posts( $query_string . "&post_type=blog_posts&post_status=publish"); ?>

    I get a blank page.

    Additionally, I was using version 1.07 of the plugin, and when I upgraded I lost the ability to have categories listed in the sidebar of my homepage if I select any post_types to also appear there. What did you guys add to 1.1.1 of the plugin that would cause that?

  • strategicblend
    • Design Lord, Child of Thor

    Quick update – while corresponding with you here, I also tested the Custom Post Type UI plugin to see if I had similar issues. I was able to use…

    query_posts('post_type=blog_posts');

    …without any issues on my template. This plugin appears to behave similarly to yours, so there must be something in Custompress prohibiting this type of query from working.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.