开发者

How to filter Wordpress posts using a hook in a plugin?

开发者 https://www.devze.com 2022-12-27 20:24 出处:网络
I\'m creating a Wordpress plugin and, being a newbie in the development on this platform, I\'m stuck on this problem.

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.

I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.

Also, due to the plugin nature, I think it make no sense to modify the theme's template.

I'm sure the s开发者_StackOverflow社区olution is evident, but can't find it!


I thought there was a nicer solution, but this is how I finally solved it:

add_filter ( 'query_vars', 'myplugin_filter_posts');

function myplugin_filter_posts( $content )
{
  //WP's query handler
  global $wp_query;

  //The id of the category whose posts I'd like to show
  $catId = 1;

  $result = $wp_query->query( 'cat='.$catId );
  return $content;
}

If you tips for a better solution, please share :)

0

精彩评论

暂无评论...
验证码 换一张
取 消