• Resolved vergedesign

    (@vergedesign)


    Hello.

    So is there a way to remove one post from showing in the post archives? I managed it fine when i wrote my own query for latest give posts (with the help that you supplied in the last ticket). The code i wrote there was

    <?php 
    query_posts(array( 
    'post_type' => 'give_forms',
    'showposts' => 10,
    'post__not_in' => array(395)
    ) );  
    ?>

    however when it comes to the post archives i dont know where to find the post query.

    I could write my own query for the archive page and create something custom but the post would still be there in the archives.

    My aim, to remove one post which is being used as a ‘quick donate’ – which will open in a modal sitewide. To give you an example… https://test7.vergedesign.co.uk

    The post in question has been removed from the home page slider of give posts… but still shows in the archive page (‘projects’)

    Massive thanks in advance.

    Best
    H

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter vergedesign

    (@vergedesign)

    okay, so ignore me, ive found the solution myself. For anyone else who wants to know… you can exclude certain posts using pre_get_posts function !

    function my_custom_get_posts( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( $query->is_archive() ) {
            $query->set( 'post__not_in', array( 1, 13, 266 ) );
        }
    }
    add_action( 'pre_get_posts', 'my_custom_get_posts', 1 );

    Basically enter the post IDs in the array and that will remove them from the archive page

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Thanks for sharing your finding @vergedesign

    I think I’d suggest one minor change, you can run that using is_post_type_archive instead so that it’s not affecting ALL your archives, but only the Give archive. See here: https://codex.www.ads-software.com/Function_Reference/is_post_type_archive

    function my_custom_get_posts( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( $query->is_post_type_archive('give_forms') ) {
            $query->set( 'post__not_in', array( 1, 13, 266 ) );
        }
    }
    add_action( 'pre_get_posts', 'my_custom_get_posts', 1 );

    Thanks for using Give!

    • This reply was modified 7 years, 4 months ago by Matt Cromwell.
    Thread Starter vergedesign

    (@vergedesign)

    Thanks, @webdevmattcrom. that is definitely an improvement. However a question… the post ID is unique over all posts regardless of their post type… is that correct? In which case, using the initial code would not affect another post type with the same post ID?

    I only ask to understand if post ID’s are unique or repeated over post types. Your code is a definite improvement and i’ve implemented I already.

    Thanks for replying.

    H

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    That’s correct, Give forms are a standard post type so they share their IDs with all your other posts. But I believe using a more specific conditional will result in quicker processing because it will simply skip over the irrelevant archives. At the micro level it’s so insignificant as to not matter at all, but if your site is getting 1000’s of hits an hour it can shave off whole seconds.

    • This reply was modified 7 years, 4 months ago by Matt Cromwell.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Remove one post from appearing in the give archives’ is closed to new replies.