Support » Plugin: Flexible Posts Widget » Filtering the query based on post type or template

  • Hi Dave,

    I love your plugin. It’s my go-to widget for listing recent posts and a lot more, so thank you.

    Currently, I’m trying to sort some posts from an events calendar plugin by closest upcoming event with a custom field. I just prefer the fine grained markup control I can get with Flexible Posts Widget over the included widget.

    I get as far as sorting the posts by the custom field, but since I have another FPW widget going as well, I need a conditional of some kind to get it to go, either by checking the post type, or even better might be the template used.

    This is what I have now that doesn’t quite work, but I feel like it’s the closest I’ve been to successful. Just wondering if you have any insight into why I am so bad at this.

    add_filter('dpe_fpw_args', 'fpw_events_args');
    
    function fpw_events_args($args) {
    	if($args['post_type'] == 'events') {
    		$args['orderby'] = 'meta_value';
    		$args['meta_key'] = 'fc_start_datetime';
    	}
    
    	return $args;
    }

    Thanks for any help! I guess I would also like to feature request ordering by meta_value and meta_value_num, with the key field popping in like it does with enabling thumbnails. Pretty niche I know, but hey, doesn’t hurt to ask.

    https://wordpress.org/plugins/flexible-posts-widget/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author DaveE

    (@dpe415)

    Hi czargyle,

    I’m not sure if that’s possible right now. I’ll look into it an see how I can it up so the filter can be applied per-widget-instance.

    I, too, have wanted a way to get posts filtered by postmeta. I’m currently exploring how I can add these fields/features to a new version of the plugin. I may end up creating a collection of different widgets to choose from depending on how one wants to get posts.

    I’ll keep you posted on the development. Cheers!

    Hey, thanks a lot for getting back to me and thanks for offering to keep my posted!

    Oddly enough, this dumb “hack” seems to have worked (after changing the widget setting, of course):

    add_filter('dpe_fpw_args', 'fpw_events_args');
    
    function fpw_events_args($args) {
    	if($args['orderby'] == 'author') { //just picking something unique
    		$args['orderby'] = 'meta_value';
    		$args['meta_key'] = 'fc_start_datetime';
    	}
    
    	return $args;
    }

    My regular recent posts are ordered by last published, and my event posts are ordered by the custom field. I’m not sure if this is a side effect of having only one author currently, but if worse comes to worse, I can just put up with the widget included in the calendar plugin.

    I doubt this is helpful, but if you have ever heard of a plugin called Custom Content Shortcode, it does some good stuff with custom loops and there might be an example or two to study. I actually would have just gone that route for my project, except I had to convert a custom field’s date and time into a different format, which could not be done with just shortcodes.

    Thanks again and good luck with future development!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filtering the query based on post type or template’ is closed to new replies.