1

I have a head-scratcher! I have a number of different post types, the default post/page/attachment + a handful created with the default CPT UI settings. Just one of them (articles) is misbehaving, with the WP_Query posts_per_page argument being ignored.

I cannot find anything about reserved post slugs, aside from the usual suspects mentioned above.

So, we have a bunch of post_type=articles. To test, I went into the DB and changed 3 of those to article - removing the s. Though I haven't created the post type in CPT IU, or in functions, WP_Query is able to return it... correctly no less!

            $a_articles__featured = array (
                'post_type'              => 'articles',
                'nopaging'                           => false,
                'posts_per_page'         => 2,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false
            );
            $q_articles__featured = new WP_Query( $a_articles__featured );

^^^ returns ALL posts with the CPTUI post_type of articles

            $a_articles__featured = array (
                'post_type'              => 'article',
                'nopaging'                           => false,
                'posts_per_page'         => 2,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false
            );
            $q_articles__featured = new WP_Query( $a_articles__featured    );

^^^ returns 2 posts with the manually adjusted post_type of article

            $a_articles__featured = array (
                'post_type'              => 'page',
                'nopaging'                           => false,
                'posts_per_page'         => 2,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false
            );
            $q_articles__featured = new WP_Query( $a_articles__featured    );

^^^ returns 2 posts with the default post_type of page

            $a_articles__featured = array (
                'post_type'              => 'events',
                'nopaging'                           => false,
                'posts_per_page'         => 2,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false
            );
            $q_articles__featured = new WP_Query( $a_articles__featured    );

^^^ returns 2 posts with the CPTUI post_type of events

ALL OF THESE RESULTS ARE REPEATED WITH THE SQL QUERY!

            $querystr = "
                SELECT $wpdb->posts.*
                FROM $wpdb->posts, $wpdb->postmeta
                WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
                AND $wpdb->posts.post_type = 'articles'
                AND $wpdb->posts.post_date < NOW()
                ORDER BY $wpdb->posts.post_date DESC
            ";

            $pageposts = $wpdb->get_results($querystr, OBJECT);

Aside from just using article, which is a disruption in the URL, can anyone suggest trouble shooting from here?

Thank you!

7
  • Articles isn't reserved (from memory). Are you absolutely sure that you don't have anything else overriding the number of posts? A filter on pre_get_posts? Apr 6, 2018 at 15:55
  • Pretty certain! I've used the _underscores theme starter, which is pretty bare bones. I searched the function file for 'articles', but nothing. Of course, this is the first post type I am trying to loop and pulled my hair out all afternoon yesterday with the usual wp_reset_postdata(); type of debugging before finding other post types are working.
    – lakewood
    Apr 6, 2018 at 16:00
  • 1
    Can you add your register_post_type call & parameters to the question? Try the query without fiddling with the cache parameters & nopaging. Apr 6, 2018 at 16:08
  • I've tried without the caching, no difference. The register_post_type is actually handled by CPT UI plugin, same that makes the events post type which works like a charm.
    – lakewood
    Apr 6, 2018 at 17:22
  • Ah - is it a setting in that plugin then? Apr 6, 2018 at 17:23

1 Answer 1

0

I deleted the CPT UI plugin and declared the post type functionally... this solved the problem.

function custom_post_type_articles() {

$labels = array(
    'name'                => _x( 'Articles', 'Post Type General Name', 'cyprusprofile' ),
    'singular_name'       => _x( 'Article', 'Post Type Singular Name', 'cyprusprofile' ),
    'menu_name'           => __( 'Articles', 'cyprusprofile' ),
    'parent_item_colon'   => __( 'Parent Article', 'cyprusprofile' ),
    'all_items'           => __( 'All Articles', 'cyprusprofile' ),
    'view_item'           => __( 'View Article', 'cyprusprofile' ),
    'add_new_item'        => __( 'Add New Article', 'cyprusprofile' ),
    'add_new'             => __( 'Add New', 'cyprusprofile' ),
    'edit_item'           => __( 'Edit Article', 'cyprusprofile' ),
    'update_item'         => __( 'Update Article', 'cyprusprofile' ),
    'search_items'        => __( 'Search Article', 'cyprusprofile' ),
    'not_found'           => __( 'Not Found', 'cyprusprofile' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'cyprusprofile' ),
);

$args = array(
    'label'               => __( 'articles', 'cyprusprofile' ),
    'description'         => __( 'Articles', 'cyprusprofile' ),
    '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( 'genres' ),
    /* 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( 'articles', $args );

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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