Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I can't get WP_Query to work in my custom-post-type archive page. Can someone please tell me what I'm doing wrong? I would appreciate any help.

Here's the code in archive-bulletin.php:

<?php get_header(); 
//wp_reset_query();
wp_reset_postdata();
$q = new WP_Query("post-type=bulletin" );
show_var("the query", $q);

if ($q->have_posts() ) : 
  while ($q->have_posts() ) : $q->the_post();
    echo "<h1>" . the_title(). "</h1>";
    the_content();
  endwhile;
endif;
wp_reset_postdata();
?>

Instead of showing my bulletin posts, it shows only post posts. The result is similar with or without the resets. (The function show_var() just wraps var_dump() in a <pre>.)

The bulletin posts show as expected with archive.php (i.e., when there's no CPT archive template). In case it's of help, here's the code I use to register the post type, etc. in the main file of my plugin:

// register bulletin post-type
add_action( 'init', 'bulletin_init' );

function bulletin_init() {
  $labels = array(
    'name' => __("Bulletins"),
    'singular_name' => __("Bulletin"),
    'menu_name' => __("Bulletins"),
    'add_new' => _x('Add New', 'bulletin'),
    'add_new_item' => __("Add New Bulletin"),
    'edit_item' => __("Edit Bulletin"),
    'new_item' => __("New Bulletin"),
    'view_item' => __("View Bulletin"),
    'search_items' => __("Search Bulletins"),
    'not_found' => __("Bulletin Not Found"),
    'not_found_in_trash' => __("No bulletins found in trash"),
 );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'show_ui' => true,
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'menu_position' => 10,
    'supports' => array( 
      'editor', 
      'excerpt', 
      'revisions'
    ),
    'taxonomies' => array( '' ),
    'has_archive' => true
  );
  register_post_type("bulletin", $args );

  add_filter('manage_edit-bulletin_columns', 'add_bulletin_columns');
  add_action('manage_bulletin_posts_custom_column', 'bulletin_columns_data', 10, 2);
  add_filter( 'manage_edit-bulletin_sortable_columns', 'bulletin_columns_sortable' );
  add_filter( 'request', 'bulletin_columns_orderby' );
}

Of course I pared down the code in archive-bulletin.php to highlight the root problem. My larger effort is to create a CPT archive page that will display the posts ordered not by date posted, but by custom meta value (bulletin_date). I've seen the post here, but it's so entangled with particulars that don't apply, that I can't get anything out of it.

EDIT

Here's the code for my pre_get_posts function that solves the larger issue:

function archive_bulletin_sort( $q ) {

    if( $q->is_post_type_archive('bulletin') ){// && $q->is_main_query() ) {
        $q->set('meta_key', 'bulletin_date');
        $q->set('orderby', 'meta_value');
        $q->set('order', 'DESC');
    }
}
add_action( 'pre_get_posts', 'archive_bulletin_sort' );
share|improve this question
2  
Just via a very quick glance: The post_type parameter needs an underscore, not a hyphen. –  Johannes Pille Apr 20 '13 at 17:10
    
Thanks, @JohannesPille. I'd certainly vote you up if I had that ability yet. –  JohnK Apr 20 '13 at 18:22
add comment

1 Answer 1

up vote 2 down vote accepted

If this is the main query, you shouldn't be creating a new query at all, just run the normal loop and you will see the posts from your CPT.

The reason your custom query isn't working is that post-type is not a valid parameter, it's post_type (underscore, not hyphen).

If your goal is to ultimately change the sort order of your CPT archive, then you're in the wrong place, nothing needs to be added to the template page, instead you should be adding a function hooked to pre_get_posts to alter the parameters of the main query.

share|improve this answer
    
Thanks, Milo! Fixing the typo resolves one mystery, and hooking pre_get_posts fixes my larger problem (I've just posted my function above for future readers). One further question, if you don't mind: you say that I'm altering the parameters of the main query; then why does the function stop working when I require is_main_query() in the conditional? I appreciate your help. –  JohnK Apr 20 '13 at 18:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.