How to use WP_Query in a CPT achive page?

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:

Read More
<?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' );

Related posts

Leave a Reply

1 comment

  1. 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.