WordPress PHP: Combining category and custom post type results

  • The aim is to combine results from a specific category with a custom post type in the results. However any results for the custom post type are never returned (the post_type name has been triple checked and is correct). Here’s my function:

    add_action( 'pre_get_posts', function ( $query )
    {
        if ( ! is_admin() ) {
            if ( isset( $query->query_vars['category_name'] ) && $query->query_vars['category_name'] === 'category-example') {
                $query->set( 'post_type', [ 'post', 'custom_post_type' ] );
            }
        }
        return $query;
    });

    Thank you kindly!

  • Hello, I’m sorry to hear that you are having trouble with your website. However, your site is not hosted with WordPress.com. It is a site using the open-source WordPress software (from WordPress.org) but hosted elsewhere.

    Because WordPress.com and WordPress.org are two entirely separate entities, we cannot access files or data for sites that are hosted elsewhere, so WordPress.com staff can only assist with sites that are hosted on our servers. You can find more information here about the differences between WordPress.org software and WordPress.com: https://wordpress.com/support/com-vs-org/

    https://wordpress.org/support/ is a great resource for sites using the open source WordPress.org software, and you can find support for that at: https://wordpress.org/support/forums/

    Your hosting provider’s support team may also be able to assist.

  • The topic ‘WordPress PHP: Combining category and custom post type results’ is closed to new replies.