1

I'm filtering my custom post type based on author. I want all users to be able to: 1 - in the list everyone can see the posts created by the admin. 2 - users can also see their own posts in the list, but not those of other users.

I can list by admin id only or current user id only, but not both. Code:

function list_cpt_by_author_id(){
    $user = new WP_User(get_current_user_id());
   
$authors_id = array(
    array('author' => "1"),
    array('author' => "$user"),
);
    
    $custom_terms = get_terms('custom-tax');
        foreach($custom_terms as $custom_term) {
            wp_reset_query();
            $args = array(
                'post_type' => 'artigos',
                'tax_query' => array(array('taxonomy' => 'custom-tax','field' => 'slug','terms' => $custom_term->slug,),),
                $authors_id,);
    
    
             $loop = new WP_Query($args);
             $image = get_field( 'cat_imagem', $custom_term );
    
             //Taxonomies Loop
             if($loop->have_posts()) {
                echo '<h2>'.$custom_term->name.'</h2>';
                echo '<img src="'.$image.'"/>';
                
                //Post loop
                while($loop->have_posts()) : $loop->the_post();
                    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
                endwhile;
             }
            wp_reset_postdata(); // reset global $post;
        }
    }

Can anyone give me any tips?

UPDATE

Thanks to @ali Sh help I did: 'author' => $adm_u_id .',' . $u_id_c

$adm_u_id so I can list any future editor ids and $u_id_c to list the userid of the current user.

2
  • 2
    Just a sidenote: You shouldn't (need to) call wp_reset_query(), only the wp_reset_postdata() that is necessary.
    – Sally CJ
    May 18 at 6:46
  • 1
    Oh! Nice. Ty again! May 18 at 18:18

1 Answer 1

2

Based on wordpress reference for WP_QUERY you can use author__in to filter by more than one author. It uses an array of user ids. Here is your code with the needed changes:

function list_cpt_by_author_id(){
    $user = get_current_user_id();
   
$authors_id = array("1", $user);

    
    $custom_terms = get_terms('custom-tax');
        foreach($custom_terms as $custom_term) {
            wp_reset_query();
            $args = array(
                'post_type' => 'artigos',
                'tax_query' => array(array('taxonomy' => 'custom-tax','field' => 'slug','terms' => $custom_term->slug,),),
                'author__in' => $authors_id,
            );
    
    
             $loop = new WP_Query($args);
             $image = get_field( 'cat_imagem', $custom_term );
    
             //Taxonomies Loop
             if($loop->have_posts()) {
                echo '<h2>'.$custom_term->name.'</h2>';
                echo '<img src="'.$image.'"/>';
                
                //Post loop
                while($loop->have_posts()) : $loop->the_post();
                    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
                endwhile;
             }
            wp_reset_postdata(); // reset global $post;
        }
    }

P.S. I also found out that you are passing WP_USER object to $authors_id, but you only need the user's id.

2
  • 2
    There's a syntax error in your code - missing array( - $authors_id = "1", "$user");. Also, the "$user" could simply be written as $user..
    – Sally CJ
    May 18 at 6:40
  • 2
    @Ali Sh Thank you very much there! You've helped me a lot, especially recommending wp_query (How did I not just search for this? LOL) In the end I did: 'author' => $adm_u_id .',' . $u_id_c $adm_u_id so I can list any future editor ids and $u_id_c to list the userid of the current user. Well you helped a lot. BTW Just need to fix a little syntax error in your answer. May 18 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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