0

I am using the function below to try and display custom post type 'direct_blog posts only. However for some reason it is also displaying standard posts and I don't know why?

Your help is much appreciated. Thanks in advance.

   function blogs_custom_author_archives_posts_per_page($query) {

       if (!is_admin() && $query->is_main_query()) {
           if ($query->is_author()) {
                $query->set('post_type', array('direct_blog'));
                $query->set('posts_per_page', 5);
            }
        }
    }
    add_action('pre_get_posts', 'blogs_custom_author_archives_posts_per_page');

1 Answer 1

0

You want to only query posts from the post-type direct_blog, but NOT the normal post type?

Than you simply need to remove post from this line:

$query->set('post_type', array('post', 'direct_blog'));

Update:

I just tested the following code.

First you need to revisit your function, as you have some errors in it.
In the code in your question, you are missing a closing }!
Than you are also using posts_per_page, but you are missing the "s", in your function it is post_per_page.

So I believe you want something like this:

function my_pre_get_posts( $query ) {

    if (!is_admin() && $query->is_main_query()) {

        if ($query->is_author()) {

            $query->set( 'post_type', array( 'my_type' ));
            $query->set( 'posts_per_page', 5 );

        }
    }
}
add_action( 'pre_get_posts', 'my_pre_get_posts' );

As I said above, this code is working in my WP testsite, Iam not seeing normal posts, just posts from my_type in my author archiv template. So try to change my_type to your post type (make sure that direct_blog is the right slug)

5
  • Thanks but I already tried that and it still also shows standard posts?
    – ianhman
    Commented Mar 24, 2017 at 14:00
  • @ianhman I added a update to my answer with code I just tested. It is working for my. Please check your function again as I saw that there are atleast 2 errors in it.
    – LWS-Mo
    Commented Mar 24, 2017 at 14:29
  • Thanks again however it stills displays the standard blog posts :( (I updated my answer to correct typos in code)
    – ianhman
    Commented Mar 24, 2017 at 14:56
  • @ianhman OK I tested it again. I copied your updated code and just changed the direct_blog to my-type. Nothing else. And this is still working in my theme. I just see posts from my-type and no normal posts. So sorry, maybe try activating another theme and see if this code is working. And you are also sure you are viewing an author archiv yes?
    – LWS-Mo
    Commented Mar 24, 2017 at 15:09
  • Thanks for all your help. In the end it turns out there was some conflicting code in another function that was causing the standard posts to show, doh!
    – ianhman
    Commented Mar 24, 2017 at 15:19

Your Answer

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

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