Is there a way of requiring category be selected before publishing the questions?

It will be a hassle to add each question to a category later on and there doesnt seem to be a default category either. Am I missing something?

  • Tom Eagles
    • Syntax Hero

    Hi there @alma_ruddock

    Have you created any question categories because this is what i get when i click on add a question.

    Or do you mean that the user doesn’t need to select the category from drop down menu to post the question?

    I am going to tag our amazing developer @hakan to see if this could be added as an option in the settings page in a future update.

    Cheers!

    Tom

  • Hakan
    • The Incredible Smush

    Hi,

    There is a hook, ‘qa_before_add_category” with which you can set a default category to new questions, if it is not already selected by the author.

    function select_default_cat ( $cat, $post ) {
    if ( 'question' == $post['post_type'] && -1 == $cat[0] )
    $cat[0] = 7;
    return $cat;
    }
    add_filter( 'qa_before_add_category', 'select_default_cat', 10, 2 );

    Add these codes to functions.php of your current theme and replace 7 with your default category ID.

    Cheers,

    Hakan

  • alma_ruddock
    • Design Lord, Child of Thor

    Hi Hakan, thanks for your response but I’ve had no luck with the code I’m afraid, questions can still be posted without a category. Take a look

    I also have another question. Is there a way to get the search to show up without the rest of the menu? I tried customizing the menu as it was but it just wasn’t working for me so I decided to use images to replace as you see but I would really have liked to keep the search.

  • Kimberly
    • Champion of Loops

    Hello Alma!

    Hi Hakan, thanks for your response but I’ve had no luck with the code I’m afraid, questions can still be posted without a category.

    Hakan’s code was meant to add a default category to the post if none was selected. Is it not working? Just wanting to clarify as from your response it seems like you are only looking to not allow the question to be published without selecting a category.

    Unfortunately as Q&A is a custom post type and uses the default WP behavior it won’t restrict on that basis. WordPress will allow a default post to be published without a category selected, so Q&A will behave similarly.

    Best :slight_smile:

    Kimberly

  • alma_ruddock
    • Design Lord, Child of Thor

    Hi Kimberly. I don’t mind if the question is published without a category, I just want it to have a default category selected (like uncategorized in normal wp) because otherwise the questions don’t show up at all when a user filters by category. Becuase questions are still coming through with no default category selected I’m having to go in every time to select one which is a bit of a hassle.

    I still have that code in my functions.php file. BTW I just saw another thread here: where a staff member has said that this feature isn’t actually available! So is this code supposed to work or not?

  • Hakan
    • The Incredible Smush

    Hi,

    I modified the above codes and tested them. It works fine for me.

    Please you update too and replace 7 with your category ID. To find the category ID select “Questions>Question Categories” and then mouse over the desired category or click Edit. The tag_ID in the link is category ID, e.g:

    http://example.com/wp-admin/edit-tags.php?action=edit&taxonomy=question_category&tag_ID=7&post_type=question

    I still have that code in my functions.php file. BTW I just saw another thread here: where a staff member has said that this feature isn’t actually available!

    Not available inside the Q&A codes, that is 100% correct. That is why I gave you the above codes to insert in functions.php of your theme.

    I also note this as a feature request to be included inside Q&A with better UI, but the above codes will also do the job.

    Cheers,

    Hakan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.