0
votes

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?

3
you should use: wp_reset_postdata();Ruhul Amin
@RuhulAmin after the loop you mean? tried that but it does not affect anything.antonanton
Better show me your complete code from archive page thenRuhul Amin
edited, i think the conflict comes from the pre_get_posts but cant figure out why exactlyantonanton
see my answer belowRuhul Amin

3 Answers

1
votes

Update your 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' );
0
votes
<?php
/*
* Creating a function to create our CPT
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Editions', 'Post Type General Name' ),
        'singular_name'       => _x( 'Edtiion', 'Post Type Singular Name'),
        'menu_name'           => __( 'Editions'),
        'parent_item_colon'   => __( 'Parent Edtiion'),
        'all_items'           => __( 'All Editions'),
        'view_item'           => __( 'View Edition'),
        'add_new_item'        => __( 'Add New Editions'),
        'add_new'             => __( 'Add New'),
        'edit_item'           => __( 'Edit Edition'),
        'update_item'         => __( 'Update Edition'),
        'search_items'        => __( 'Search Edition'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash'),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'edition' ),
        'description'         => __( 'Editions and edition specific data' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies' => array( 'post_tag','edition'),   
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'edition', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );

function themes_taxonomy() {  
    register_taxonomy(  
        'edition',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'edition',        //post type name
        array(  
            'hierarchical' => true,  
            'label' => 'Edition store',  //Display name
            'query_var' => true,
            'show_ui'           => true,
            'show_admin_column' => true,
            'rewrite' => array(
                'slug' => 'edition', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before 
            )
        )  
    );  
}  
add_action( 'init', 'themes_taxonomy');

?>
-1
votes

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' );