0

I want to get posts from a particular category or if the post by a particular user . It seems we could only have AND condition in wordpress . I know the below code is wrong but this is what I need to get - I want all the posts written by a particular user OR all the posts from a particular category

$args = array(
    'posts_per_page'   => 10,
    'offset'           => $PageStart,
    'query' => array(
        'relation' => 'OR', /* <--                here */
        array(
            'author' => 18,
        ),
        array(
            'category' => 20,
        )
    ),
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
);
//print_r($args);
$author_post = get_posts( $args );
1
  • 1
    Please properly clean up and indent your code before posting. It is quite hard reading messy code that is all over the place. I have properly formatted your code to make it more readable Commented Jul 10, 2015 at 11:35

1 Answer 1

0

I used the following in my child theme 'functions.php' to get posts by category name. You can also use category ID number but that didn't work for me.

add_action('pre_get_posts', 'ad_filter_categories');
function ad_filter_categories($query) {
if ($query->is_main_query() && is_home()) {
    $query->set('category_name','tips');
 }
}

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.