Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Herman Asrori (BSF)

    (@bsfherman)

    Hi @heyjoecampbell,

    We’re sorry for the delay.

    You should be able to do that using a filter in get_option(). Please refer here.

    I hope it will help.

    Kind regards,
    Herman ??

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Hi Herman,

    Please provide a cut & paste solution that I can use with the Snippets plugin for the CPT “announcements”.

    Thanks,

    Joe

    Plugin Support Herman Asrori (BSF)

    (@bsfherman)

    Hi @heyjoecampbell,

    Actually, we don’t provide custom code as it is beyond our support and we usually will let the community takes the part if the query involves custom code.

    However, please try the following

    function unlist_posts() {
    	
    	$query = new WP_Query( array( 'post_type' => 'announcements', 'posts_per_page' => -1 ) );
    	$array_of_post_type_announcements = [];
    	if ( $query->have_posts() ) {
    		while ( $query->have_posts() ) {
    			$query->the_post();
    			$post_id = get_the_ID();
    			array_push($array_of_post_type_announcements, $post_id );
    		}
    	}
    	$hidden_posts = update_option( 'unlist_posts', $array_of_post_type_announcements );
    
    	return $hidden_posts;
    }
    add_action( 'init', 'unlist_posts' );

    You need to add the code to your child theme’s functions.php.

    I hope it will help.

    Kind regards,
    Herman ??

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Thanks Herman ?? greatly appreciated

    Plugin Support Herman Asrori (BSF)

    (@bsfherman)

    You’re most welcome, @heyjoecampbell! We are pleased could be a help.

    I am marking this thread as Resolved. Feel free to open a new topic if you need further assistance.

    Kind regards,
    Herman ??

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Happy New Year ??

    One last question – how should I modify the unlist multiple CPTs (announcements, reviews, hobbies)

    Plugin Support Herman Asrori (BSF)

    (@bsfherman)

    Try this @heyjoecampbell

    function unlist_posts() {
    	
    $query = new WP_Query( array( 'post_type' => array( 'announcements', 'reviews', 'hobbies', 'posts_per_page' => -1 ) );
    	$array_of_post_type_announcements = [];
    	if ( $query->have_posts() ) {
    		while ( $query->have_posts() ) {
    			$query->the_post();
    			$post_id = get_the_ID();
    			array_push($array_of_post_type_announcements, $post_id );
    		}
    	}
    	$hidden_posts = update_option( 'unlist_posts', $array_of_post_type_announcements );
    
    	return $hidden_posts;
    }
    add_action( 'init', 'unlist_posts' );

    I hope it helps.

    Kind regards,
    Herman ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Code Snippet to Unlist a CPT’ is closed to new replies.