• Resolved studiokb

    (@studiokb)


    Hi,
    Great plugin. I was wondering if you can somehow exclude the current post. I would often use a carousel in a template file for certain custom post types and have the carousel showing at the bottom, like Our Team. It would be great if I could ask it to exclude the current post so the person does not appear in the carousel on their own page.
    This might be possible using the WordPress query parameter post__not_in()

    Would be a great addition.
    Thanks

    https://wordpress.org/plugins/wp-posts-carousel/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter studiokb

    (@studiokb)

    Oh I see you have a filter on the query – wpc_query (2 parameters – $value, $params) *
    Maybe I can use this to get what I want? I would maybe need to create a variable with current post ID and drop it into this filter?
    Would that work?

    Plugin Author teastudio.pl

    (@teastudiopl)

    yes, please use “wpc_query” filter and add “post__not_in” value with the current post ID.

    Thread Starter studiokb

    (@studiokb)

    Hi,
    I wonder could you help with this code to exclude an ID. I’m just trying it by setting my exclude ID to the post ID I want to exclude, 214 in this case.
    This is what i have been trying, there’s another attempt commented out:

    function ri_wpc_exclude_current( $query_args, $params ) {
        $exclude_ids = array( 214 );// 
    
        $query_args['post__not_in'] = $exclude_ids;
        //$query_args['post_not_in'] = explode(',', $params['214']);
    }
    add_filter('wpc_query', 'ri_wpc_exclude_current', 1, 2);

    But I get a warning
    Warning: Missing argument 2 for ri_wpc_exclude_current()

    I tried removing $params from the ri_wpc_exclude_current parameters but that doesn’t work.

    Plugin Author teastudio.pl

    (@teastudiopl)

    try this:

    add_filter('wpc_query', 'ri_wpc_exclude_current', 1, 1);

    Thread Starter studiokb

    (@studiokb)

    Hi,
    Thanks. I still get the error – Missing argument 2 for ri_wpc_exclude_current()

    with:

    function ri_wpc_exclude_current( $query_args, $params ) {
        $exclude_ids = array( 214 );//
        $query_args['post__not_in'] = $exclude_ids;
    }
    add_filter('wpc_query', 'ri_wpc_exclude_current', 1, 1);

    I’m not hugely familiar with filtering an already existing wpQuery.

    Plugin Author teastudio.pl

    (@teastudiopl)

    sorry, it’s my mistake.

    The correct version is:

    function get_query(  $query, $params ) {
    	echo '<pre>';
    		print_r($query);
    	echo '</pre>';
    	exit;
    
    	return $query;
    }
    
    add_filter('wpc_query', 'get_query', 1, 2);

    and it’s working on the latest version.

    Where did you paste this code, in your functions.php file?

    Thread Starter studiokb

    (@studiokb)

    Hi,

    I’m actually putting the code in a custom-post-type single page (trying to exclude the post I’m on from showing in carousel). But i’ve tried in the functions file and get the same warning here Warning: Missing argument 2 for get_query() in and it references the first line where the function is declared.

    Think we’re getting closer though, printing out the query array helps. My code now is :

    function get_query(  $query, $params ) {
        $exclude_ids =  214;
    	echo '<pre>';
    		print_r($query);
    	echo '</pre>';
        $query['post__not_in'] = $exclude_ids;
        echo '<pre>';
    		print_r($query);
    	echo '</pre>';
    	exit;
    
    	return $query;
    }
    add_filter('wpc_query', 'get_query', 1, 2);

    And I get printed out on the page:

    Array
    (
        [post_type] => Array
            (
                [0] => head-office-team
            )
    
        [post_status] => publish
        [posts_per_page] => 8
        [no_found_rows] => 1
        [post__not_in] => Array
            (
                [0] =>
            )
    
        [orderby] => ID
        [order] => asc
    )
    Array
    (
        [post_type] => Array
            (
                [0] => head-office-team
            )
    
        [post_status] => publish
        [posts_per_page] => 8
        [no_found_rows] => 1
        [post__not_in] => 214
        [orderby] => ID
        [order] => asc
    )

    Head-office-team is the custom post type and I’ve printed the query twice to see if I was assigning the value correctly. It looks to be assigned to post__not_in (in second print out) but the warning Missing argument 2 for get_query() shows and the carousel doesn’t load, nor does the rest of the page.
    Do I need to do something with $params?

    Plugin Author teastudio.pl

    (@teastudiopl)

    could you send me any ftp access to your theme/plugins dir ?
    m.gierada@teastudio.pl

    Thread Starter studiokb

    (@studiokb)

    Hey,
    I got it to work.
    I had to target the post__not_in part of the query and make sure the input was an array. Also, I never notice before but the exit; was obviously just exiting the function so the rest of my page was not loading. This my code now:

    function filter_query(  $query ) {
        $exclude_ids =  "212";
    	echo '<pre>';
    		print_r($query);
    	echo '</pre>';
        $query['post__not_in'] = array(212, 214);
        echo '<pre>';
    		print_r($query);
    	echo '</pre>';
    
    	return $query;
    }
    add_filter('wpc_query', 'filter_query', 1 );

    I was still getting the warning, but by removing the $param arguments that was prevented.
    Now I just need to dynamically get the page id into my variable. Should be easy…
    Thanks for your help.

    PS. for anyone else, remove the echo and print statements from above.

    Plugin Author teastudio.pl

    (@teastudiopl)

    close

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Can you exclude current Post’ is closed to new replies.