Add filter for specific post type only

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

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
  • listing/grid/posts-query-args Is not a core a WordPress hook, so it’s very difficult to say what the proper way to use it is without documentation. – Jacob Peattie Commented Oct 11, 2019 at 14:26
  • This part is not important in this case, because this code works. I need this achieve 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'); – heroj Commented Oct 11, 2019 at 14:30
  • Yes, it is important to know what the hook does. I can't tell from your code what $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 what listing/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
  • No this is only name of filter. – heroj Commented Oct 11, 2019 at 14:44
  • As I wrote I have this working function, but it works for whole website, and I need It only for CPT "events". – heroj Commented Oct 11, 2019 at 14:45
Add a comment  | 

3 Answers 3

Reset to default 1

There'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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745086432a4610432.html

相关推荐

  • Add filter for specific post type only

    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

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信