Function to include Q&A CPTs in global search

Solved3.65K viewsCorerewrite
0

Hi all!

By default, “question” and “answer” post types are excluded from search (set to “true” in includes/post-types.php) and I’m trying to override the exclude_from_search argument. From what I understood, I just need to register again the post type in my functions.php and change the argument to what I want. So I tried this but it doesn’t seem to work:

function update_question_init() {
$args = array(
‘exclude_from_search’ => false,
);

register_post_type( ‘question’, $args );
}

add_action( ‘init’, ‘update_question_init’ );

I also tried to delay the init by using add_action( ‘init’, ‘update_question_init’, 1000 ); but no luck either…

Any idea on what I’m doing wrong?

Question is closed for new answers.
selected answer
1

Hello Fred,

I will suggest not to do this. As it may break question permalink. But AnsPress provides a hook to let you modify CPT args. Check this:

ap_question_cpt_args

Please avoid changing other arguments except search.

commented on answer

Thanks Rahul, exactly what I needed! Sure, I don’t wanna mess with the CPT, just include it in search results. 😉 Anyway, I first need to figure out how to use the apply_filter() function. ^^;

@Rahul Really sorry to bother you again but I have tried quite a few combinations (after reading the Codex for filters and also this page (http://dev.themeblvd.com/tutorial/filters/) but I just can’t find a way to use the hook. I’m sure it’s just a matter of putting everything in the right order but I can’t figure it out. Do you have any working example at hand? About this particular hook or any filter that modifies an argument inside an array. I’ll keep checking on my side so if you don’t have time, no worries. 😉

Thanks Rahul! It’s working great and helping me understand a bit more how filters work. 😉 So, just to make sure, by using ap_question_cpt_args (and ap_anser_cpt_args) I don’t need the “init” hook?