You are viewing an old revision of this post, from August 4, 2015 @ 05:18:53. See below for differences between this version and the current revision.

Modifying queries with pre_get_posts

Written by Remi Corson. you can for example query only posts from a specific type and with a custom meta field key equal to a determined value. In the code below, we are targeting only custom post type called “portfolio” where meta key “project_type” is set to “design”, that’s a typical query that can be used on a portfolio page. [php]// Load our function when hook is set add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' ); function rc_modify_query_get_design_projects( $query ) { // Check if on frontend and main query is modified if( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] != 'portfolio' ) { $query->set('meta_key', 'project_type'); $query->set('meta_value', 'design'); } }[/php]

Revisions

  • August 4, 2015 @ 05:18:53 [Current Revision] by PeterLugg
  • August 4, 2015 @ 05:18:53 by PeterLugg

Revision Differences

There are no differences between the August 4, 2015 @ 05:18:53 revision and the current revision. (Maybe only post meta information was changed.)

Tags: ,

No comments yet.

Leave a Reply