My filter works good, but for all pages and I need add filter only for specific post type. I try it via is_singular, but not work. My code:
function my_super_filer_function6($query_args){
if ( is_singular( 'post' ) ) {
global $post;
$post_author = $post->post_author;
$query_args['author'] = $post_author;
return $query_args->is_singular( $post_types );
}
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
EDIT: in other words how achieve this working code for specific post type:
function my_super_filer_function6($query_args){
global $post;
$post_author = $post->post_author;
$query_args['author'] = $post_author;
return $query_args;
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
My filter works good, but for all pages and I need add filter only for specific post type. I try it via is_singular, but not work. My code:
function my_super_filer_function6($query_args){
if ( is_singular( 'post' ) ) {
global $post;
$post_author = $post->post_author;
$query_args['author'] = $post_author;
return $query_args->is_singular( $post_types );
}
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
EDIT: in other words how achieve this working code for specific post type:
function my_super_filer_function6($query_args){
global $post;
$post_author = $post->post_author;
$query_args['author'] = $post_author;
return $query_args;
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
Share
Improve this question
edited Oct 11, 2019 at 14:37
heroj
asked Oct 11, 2019 at 14:24
herojheroj
238 bronze badges
5
|
3 Answers
Reset to default 1There's a couple ways you could handle this.
Option 1 Check the global $post
object
Inside your filter you may be able to check the current $post
object or other Conditional Tags for a specific type.
function my_super_filer_function6( $args ) {
global $post;
/* Check against the current post object */
if( empty( $post ) || 'post_type_here' !== $post->post_type ) {
return $args;
}
/* Check conditional tags */
if( ! ( is_post_type_archive( 'post_type_here' ) || is_singular( 'post_type_here' ) ) ) {
return $args;
}
/* ... */
}
Option 2 Add the hook via template_redirect
or an early hook
You may be able to add the hook via an earlier action hook:
function wpse350295_init() {
if( is_post_type_archive( 'post_type_here' ) || is_singular( 'post_type_here' ) ) {
add_filter( 'listing/grid/posts-query-args', 'my_super_filer_function6' );
}
}
add_action( 'template_redirect', 'wpse350295_init' );
Option 3 Use the args
You may be able to check the passed args for a post_type
and return early if the post type does not match what is expected:
function my_super_filer_function6($query_args){
if( 'post_type' !== $query_args->get( 'post_type' ) ) {
return $query_args
}
/* ... */
}
This is of course assuming what is being passed is a WP_Query
object. You would need to do some due diligence to figure out what the passed variable holds and if a post type can be discerned from it.
This will only execute that code for a specific post type:
function my_super_filer_function6($query_args){
global $post;
if($post->post_type == 'specific post type'){
$post_author = $post->post_author;
$query_args['author'] = $post_author;
}
return $query_args;
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
Here is working result from reply above:
function my_super_filer_function6( $query_args ) {
global $post;
if($post->post_type == 'events'){
$post_author = $post->post_author;
$query_args['author'] = $post_author;
}
return $query_args;
}
add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
function my_super_filer_function6($query_args){ global $post; $post_author = $post->post_author; $query_args['author'] = $post_author; return $query_args; } add_filter('listing/grid/posts-query-args', 'my_super_filer_function6');
– heroj Commented Oct 11, 2019 at 14:30$query_args
is necessarily supposed to be, or where the hook runs.return $query_args->is_singular( $post_types );
is almost certainly nonsense, but I can't tell because there's no information on whatlisting/grid/posts-query-args
is supposed to be or do. I can't even tell from your question what post type you want to check. Is it the post type that this grid is embedded in? The post type that's listed in the grid? – Jacob Peattie Commented Oct 11, 2019 at 14:36