1

I currently have a custom post type named "blog". On registration, every user gets automatically a post "blog" linked with their user ID.

This custom post type queries another post type named "articles". Those articles are written by the user and then showed in their "blog" custom post type.

Now, I would like to show a list of X most recent "blog" custom post type but only if they have at least one "article" in it but I can not find any way to do it...

Thank you in advance for your help and have a good day.

4
  • What do you mean by having an "Article" CPT in a "blog" CPT? How are they linked?
    – s_ha_dum
    Commented Apr 4, 2014 at 13:52
  • The only link they have it's the author_ID. What I meant it's that the custom-post-type-blog template queries custom-post-type-articles if both have the same author. Commented Apr 4, 2014 at 14:03
  • 2
    So if I'm correct, you want to know how you can create a WP_Query to query the latest blog (CPT) posts from users that have at least one article (CPT). Is this correct?
    – engelen
    Commented Apr 4, 2014 at 16:22
  • 1
    That's correct! :) Commented Apr 4, 2014 at 21:17

2 Answers 2

2

I finally found a workaround for this, here it goes :

I used "get_user" with a "has_custom_post_type" function to query every user with at least one article written and then added the link to their blog CPT by adding "/blog/display_name" in the href.

Here goes the code

<?php
$blog_url = get_bloginfo('home');
$users_number = 0;
$blogusers = get_users('orderby=registered&order=DESC');
    foreach ($blogusers as $user) {
     $cpt_count = has_custom_post_type( $user->ID, 'article' );
  if (!empty($cpt_count) ) {
    $users_number ++;
    if ($users_number <= 5) {
      echo '<li><a href="' . $blog_url . '/blog/' . $user->user_nicename . '">Le blog de ' . $user->display_name . '</a></li>';
    }
  }
}
?>

Thanks to everyone for your help.

0

I haven't tested this, but in my head it works perfectly! What we do is put add a pre_get_posts filter in our functions file then get the current user. IF user isn't registered, drop out. IF the number of user posts isn't greater than 0, drop out. IF we're not on the post type archive, drop out. IF all 3 conditions are true, we'll replace our default blog articles with the users.

function user_posts_filter($query){
    $userID = get_current_user_id();

    if($userID != 0 && count_user_posts($userID) > 0 && is_post_type_archive('blog')){
        $query->set('post_type', 'articles');
        $query->set('author', $userID);
    }
}
add_action('pre_get_posts', 'user_posts_filter');

Hopefully it works for you!

1
  • Thank you for your answer! But what I'd like to do is to query the CPT-blog of any author (there is only one per author) having written at least one CPT-article, and then display them like a list on various pages (so not only on the CPT archive). Commented Apr 4, 2014 at 22:04

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.