Home > database >  Get the author's posts in spacific category
Get the author's posts in spacific category

Time:10-27

i want to get the author's posts in spacific category and display it in author page I tried this code and it didn't work, what is the problem? my code :

<?php
$url= get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
    'author' => $author_id,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'cat' => '161,181,157',
            'post_count' => '3',
        ),
    ),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
    $query->the_post();
    $postid = get_the_ID();
    echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
?>

CodePudding user response:

Try the below code.

$url = get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
    'author' => $author_id,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array(161,181,157)
            
        ),
    ),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
    $query->the_post();
    $postid = get_the_ID();
    echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
  • Related