Search code examples
phpsqlwordpressfunctioncustom-post-type

WordPress Querying Custom Post Type


I need a bit of help querying some posts with a specific category from a custom post type as it doesn't seem to be working. Below is the code in my functions.php file which registers the post type of Event & a custom Taxonomy of Event Category there so it separates it from the usual "Post" categories.

function cpt_events() {
    $labels = array(
        'name'               => 'Events',
        'singular_name'      => 'Event',
        'menu_name'          => 'Events',
        'name_admin_bar'     => 'Event',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Event',
        'new_item'           => 'New Event',
        'edit_item'          => 'Edit Event',
        'view_item'          => 'View Event',
        'all_items'          => 'All Events',
        'search_items'       => 'Search Events',
        'parent_item_colon'  => 'Parent Event',
        'not_found'          => 'No Events Found',
        'not_found_in_trash' => 'No Events Found in Trash'
  );

  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'show_ui'             => true,
    'show_in_nav_menus'   => true,
    'show_in_menu'        => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-calendar-alt',
    'capability_type'     => 'post',
    'hierarchical'        => false,
    'supports'            => array( 'title', 'author', 'thumbnail' ),
    'has_archive'         => true,
    'rewrite'             => array( 'with_front' => false, 'slug' => 'events' ),
    'query_var'           => true
  );

  register_post_type( 'events', $args );
}
add_action( 'init', 'cpt_events' );

function events_tax() {
  $args = array(
    'hierarchical' => true
  );
  register_taxonomy( 'events_category', 'events', $args );
}
add_action( 'init', 'events_tax', 0 );

I then also have the code that I'm trying to use to query the posts:

$events_args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'category_name' => 'of-rifle-club1',
    'posts_per_page' => 3,
    'orderby' => 'date', 
    'order' => 'DESC'
);
$loop = new WP_Query($events_args);

I've used this argument for the default posts in WordPress, but it doesn't work for the above custom post types and taxonomies unfortunately:

'category_name' => 'OF Rifle Club'

That's essentially what I'm trying to do, only query posts that have a category name of "OF Rifle Club".

Any help would be much appreciated. Thanks!


Solution

  • use tax_query. check the below code.

    $events_args = array(
        'post_type'      => 'events',
        'post_status'    => 'publish',
        'posts_per_page' => 3,
        'orderby'        => 'date', 
        'order'          => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy' => 'events_category',
                'field'    => 'slug',
                'terms'    => 'of-rifle-club1',
            ),
        )
    );
    
    $loop = new WP_Query( $events_args );