billerickson/BE-Subpages-Widget

Running on posts via filter

Closed this issue · 3 comments

I need to display child pages of a particular page on posts (an event CPT) however the plugin bails if not on a hierarchical post type (line 53) which normally makes sense...

I tried a couple things and arrived at this (starting at be-subpages-widget.php:51):

// Only run on hierarchical post types unless passed a 'post_type'
$page_filter_check = apply_filters( 'be_subpages_widget_args', $args );
$post_types = get_post_types( array( 'hierarchical' => true ) );
if ( !is_singular( $post_types ) && !isset($page_filter_check['post_type'] ) )
    return;

Seems to be working, but feel like I might be missing something. What are your thoughts on that?

for reference I'm passing into the filter this:

add_filter( 'be_subpages_widget_args', 'touro_subpages_widget_args' );
/**
 * Add Parent to the Subpages Widget.
 *
 * @param array $args Array of arguments.
 * @param array Modified array of arguments for get_pages().
 */
function touro_subpages_widget_args( $args ) {

    $post_type = get_post_type( get_the_ID() );
    if ( is_page('calendar') || 'ai1ec_event' == $post_type ) { //TODO Not working for single CPT due to be-subpages-widget.php:53 limit to hierarchical post types.
        // Build a menu listing top level parent's children
        $args['child_of'] = 2;
        $args['parent'] = 2;
        $args['sort_column'] = 'menu_order';
        $args['post_type'] = 'page';
    }

    return $args;
}

I've added an override filter. Use it like this:

/**
 * Display Subpages on Events posts
 *
 */
function be_subpages_widget_on_events( $override ) {
    if( is_singular( 'events' ) )
        $override = true;
    return $override;
}
add_filter( 'be_subpages_widget_display_override', 'be_subpages_widget_on_events' );

Let me know if that works for you.

Cool, I'll give it a try. I'd thought about adding a second (3rd) filter like that, but it seemed better to to me to just check if the post_type was set since in order to work on a post there would have to be a page passed in. Seems like with this I'd have to pass in the 'page' post_type and tell it to override separately. Doesn't matter much to me just discussing.

Correct, you'll also need to specify the query args separately. I think this approach provides more flexibility for other odd uses.