I am having a conflicting issue with a custom post type. On my archive pages, the custom post type was not showing up with the general posts, so I had to add in this pre_get_posts
filter:
add_action( 'init', 'editions' );
function editions() {
$labels = array(
'name' => _x( 'Editions', 'post type general name' ),
'singular_name' => _x( 'Edtiion', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Edition' ),
'edit_item' => __( 'Edit Edition' ),
'new_item' => __( 'New Edition' ),
'all_items' => __( 'All Editions' ),
'view_item' => __( 'View Edition' ),
'search_items' => __( 'Search Editions' ),
'not_found' => __( 'No editions found' ),
'not_found_in_trash' => __( 'No editions found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Editions',
);
$args = array(
'labels' => $labels,
'description' => 'Editions and edition specific data',
'menu_icon' => 'dashicons-building',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
'taxonomies' => array( 'edition', 'post_tag'),
'has_archive' => true,
);
register_post_type( 'edition', $args );
}
add_action( 'init', 'register_taxonomy_for_edition' );
function register_taxonomy_for_edition() {
register_taxonomy_for_object_type( 'post_tag', 'edition' );
};
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if( !is_admin() ) {
if ( !is_post_type_archive() && $query->is_archive() ) {
$query->set( 'post_type', array( 'post', 'edition' ) );
}
return $query;
}
}
This does what I want it to do, and in the archive page shows the general posts, and a custom post type I defined called 'Edition'
However, when I try to make a custom query to only call the custom post type, and no other posts... it just returns ALL posts regardless of the post_type definition i set:
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'edition',
'posts_per_page' => -1
);
$allgemeine = new WP_Query( $args );
while ( $allgemeine->have_posts() ) : $allgemeine->the_post();
the_content();
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
When I remove the pre_get_posts filter from mzy functions.php
, the loop works perfect, but the custom post types do not show in the archive page.
Any ideas why this conflict is happening?
A note to people who might use this, I had a problem with this filter:
function my_get_posts( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array( 'post', 'nav_menu_item', 'edition' ));
return $query;
}
}
add_filter( 'pre_get_posts', 'my_get_posts' );
It worked for the most part but was causing a third party conflict with Advanced Custom Fields where it was breaking a search function through the database. I modified the logic to avoid modification to 3rd party get_posts calls:
function my_get_posts( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_archive) {
$query->set( 'post_type', array( 'post', 'nav_menu_item', 'edition' ) );
return $query;
}
}
}
add_filter( 'pre_get_posts', 'my_get_posts' );