0
votes

I have two categories in WordPress. One is events and another one is news. I have to fetch posts in two categories in same page. There is only 4 posts in news category. And events can add to user. I have to display first 8 events based on the date of post. I am writing both categories using two queries and moving to an array.

I've coded for this as given below:

$event_title       = array();
$event_author      = array();
$event_content     = array();
$event_thumbnail   = array();
$event_counter     = 0 ;

$arg = array(
    'numberposts' => 8,
    'offset' => 0,
    'category' => 17,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'post',
    'post_status' => 'draft, publish, future, pending, private',
    'suppress_filters' => true
);

$events = new WP_Query( $arg );

if ($events->have_posts()) : while ($events->have_posts()) : $events->the_post(); 

$event_title[$event_counter]        = get_the_title();
$event_author[$event_counter]       = get_the_author();
$event_content[$event_counter]      = get_the_content();
$event_thumbnail[$event_counter]    = get_the_post_thumbnail();

$event_counter++;

endwhile; endif;

the category id for events is 17, I've find the id using the method echo get_cat_ID( "events" );

the problem here is the posts are not fetched on the basis of category. it takes first 8 posts withou considering the category. how can I solve this issue.

1

1 Answers

1
votes

In your code you use $arg, with 'category' => 17, but in codex is written:

cat (int) - use category id.
category_name (string) - use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id. 

So you need use cat for number of category or use array with category__in