in my Wordpress plugin i used the post_where filter but it's effect all other post type of my site.
my question is how to set this filter only for "property" post only
add_filter('posts_where', 'posts_where');
function posts_where($where)
{
global $wpdb,$wp_query;
$where .= ' AND latitude.meta_key="wp_gp_latitude" ';
$where .= ' AND longitude.meta_key="wp_gp_longitude" ';
return $where;
}
in my Wordpress plugin i used the post_where filter but it's effect all other post type of my site.
my question is how to set this filter only for "property" post only
add_filter('posts_where', 'posts_where');
function posts_where($where)
{
global $wpdb,$wp_query;
$where .= ' AND latitude.meta_key="wp_gp_latitude" ';
$where .= ' AND longitude.meta_key="wp_gp_longitude" ';
return $where;
}
Share
Improve this question
asked Jan 8, 2015 at 10:10
Ankur BhadaniaAnkur Bhadania
911 silver badge10 bronze badges
1
- It is unclear what you want. Please file an edit and explain what do you mean you want the filter only for custom post types. How are you using the filter and what is the expected out come – Pieter Goosen Commented Jan 8, 2015 at 11:16
2 Answers
Reset to default 1Please note that the post_Type query variable is not set for standard types (page, post and attachment, see WP_QUery class file line 2396 on trac), only for custom post types, so this is how you can test for each type,
add_filter( 'posts_where' , 'posts_where', 10, 2);
function posts_where( $args, $wp_query_obj ) {
$type = $wp_query_obj->query_vars['post_type'];
switch(true){
case 'any'==$type: //query any type (see codex for more details).
break;
case !empty($type) && is_array($type):
//multiple post types define
break;
case !empty($type):
//post type is a custom post.
//this is where you would check for your post type,
if ($type == 'property') {
$args .= ' AND latitude.meta_key="wp_gp_latitude" ';
$args .= ' AND longitude.meta_key="wp_gp_longitude" ';
}
break;
case $wp_query_obj->is_attachment():
//post type is empty but is a media.
$type='attachemnt';
break;
case $wp_query_obj->is_page():
//post type is empty but is a page.
$type='page';
break;
default:
//post type is empty and not an attachment nor a page, so is a post.
$type='post';
break;
}
return $where;
}
for more information on the WP_Query object and its methods, see the codex page.
add_filter( 'posts_where' , 'posts_where', 10, 2);
function posts_where( $where, $query ) {
global $wpdb,$wp_query;
if ($query->query_vars['post_type'] == 'property') {
$where .= ' AND latitude.meta_key="wp_gp_latitude" ';
$where .= ' AND longitude.meta_key="wp_gp_longitude" ';
}
return $where;
}
If you add $priority = 10 //normal
and $accepted_args = 2
to your add_filter
function,
you will have a $query
object in your posts_where
function that you can put a conditional on your post_type
for 'property' to have it only affect your custom post type.
Here is a link to the WP codex documentation for the add_filter
function:
http://codex.wordpress/Function_Reference/add_filter