0

I have a created a custom post type called "Events" using register_post_type. I am now trying to create a submenu within "Events" that will only show events sorted by a specific meta_key and meta_value. (Example: Only show events that have been marked as "priority.")

I am using this code to create the "priority events" submenu, but the query continues to return all of my events instead of just the priority events:

function rt_priority_event_add_submenu() {

$sub_slug = 'edit.php?post_type=event&meta_key=event_priority&meta_value=true';
add_submenu_page('edit.php?post_type=event', 'Priority Events', 'Priority Events', 'manage_options', $sub_slug); 

}

Here is the Events custom post type is set up:

register_post_type( 'event', array(
        'label' => 'Events',
        'labels' => rt_ui_labels( 'Event', 'Events', 'redtri' ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail'),
    ) );

I don't need any new fancy columns or anything - I just want to use the same columns already set up in Events.

Thanks in advance, and please let me know if you need more information!

0

Meta key or meta value in query parameter doesn't automatically filter the query by default. So you have to filter the query to restrict results.

add_filter('pre_get_posts', 'restrict_events_by_meta');
function restrict_events_by_meta($q){
    if( 
        $q->is_main_query() 
        && is_admin() 
        && 'event' == $q->get('post_type') 
        && isset( $_GET['meta_key']) 
        && isset( $_GET['meta_value']) 
    ){
        $q->set('meta_key', $_GET['meta_key'] );
        $q->set('meta_value', $_GET['meta_value'] );
        $q->set('orderby', 'meta_key' );
    }
}
  • Ah-ha! YES! Thank you so much, I have been banging my head over this one for about a week. In case no one has said it lately, you RULE. :) – shutterpixel Dec 10 '13 at 21:26

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.