0

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');
  • 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 Oct 11 '19 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 Oct 11 '19 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 Oct 11 '19 at 14:36
  • No this is only name of filter. – heroj Oct 11 '19 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 Oct 11 '19 at 14:45
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.

| improve this answer | |
  • Thank You, It works and help me lot. – heroj Oct 11 '19 at 15:34
1

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');
| improve this answer | |
  • Thank You, It works and help me. – heroj Oct 11 '19 at 15:34
0

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');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.