• Resolved Hisham

    (@ctrlsync)


    Hello all, thanks in advanced.

    I am setting up a custom post type for a restaurant owner and have a section on the home page that pulls the most recent post under the category daily-specials. The custom post code works fine and I have the ability to check daily-specials as a category but it does not show up as the most recent daily-special post on the home page.

    Here is the code I am using in my divi child them:

    // custom post type function
    function create_posttype() {
      
        register_post_type( 'Specials',
        // CPT Options
            array(
                'labels' => array(
                    'name' => __( 'Specials' ),
                    'singular_name' => __( 'Special' )
                ),
                'public' => true,
                'has_archive' => true,
                'rewrite' => array('slug' => 'daily-specials'),
                'show_in_rest' => true,
    			'taxonomies' => array( 'category' ),
      
            )
        );
    }
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype' );

    What am I missing to allow these post types to be seen by the Divi blog module as the most recent daily-specials post?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • It looks like you’ve set up your custom post type correctly with the ability to use categories, including ‘daily-specials’. However, to ensure that these posts show up as the most recent in the Divi blog module, you might need to adjust the query used by Divi to fetch these posts. By default, the Divi blog module may only be set up to fetch regular posts, not custom post types.

    Here are a couple of things you can check or try:

    1. Divi Module Settings: First, ensure that the Divi blog module is configured to display posts from the ‘daily-specials’ category. Sometimes, it might be set to display posts from all categories or a different specific category.
    2. Modify Query for Divi Blog Module: If the Divi blog module doesn’t automatically include your custom post type, you might need to modify the query it uses. This can often be done by hooking into the pre_get_posts action in WordPress. Here’s an example snippet you can add to your theme’s functions.php:

    function include_custom_post_types_in_divi( $query ) { if ( ! is_admin() && $query->is_main_query() && is_home() ) { $query->set( 'post_type', array( 'post', 'Specials' ) ); } } add_action( 'pre_get_posts', 'include_custom_post_types_in_divi' );

    Check Category Assignment: Ensure that your ‘Specials’ posts are correctly assigned to the ‘daily-specials’ category. Sometimes, issues can arise if the category assignment isn’t as expected.

    Divi Theme Customization: If the standard settings and hooks don’t work, you might need to look into more specific customizations for Divi. This could involve creating a custom loop or template file in your child theme that specifically queries and displays your ‘Specials’ post type.

    Divi and Third-Party Plugins: If you’re using any third-party plugins that modify the behavior of the Divi blog module or post queries in general, ensure that they are not conflicting with your setup.

    Thread Starter Hisham

    (@ctrlsync)

    Thanks for your reply.

    I have tried the above code snipped with no luck. The blog module on the homepage is selected to show 1 post (most recent) from the Daily Specials category. The test post I made shows in the Specials list with category of Daily Specials.

    As far as plugins, I have woocomerce, divi tourque lite, wp super cache, and a custom plugin (Ctrl Login) I use to white label the login screen and admin area.

    I tried creating a test page using default editor to remove Divi from the equation, when I use the post list block and set Daily Specials to the category I still dont see my test special post that I created. This makes me thing its not being registered correctly with WordPress?

    Thanks.

    • This reply was modified 10 months, 2 weeks ago by Hisham.
    Thread Starter Hisham

    (@ctrlsync)

    Ok after some googling around since I knew it was not a Divi specific issue I found a bit of code added to the child theme below the custom post code to make it all work.

    function special_add_custom_types( $query ) {
       if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
          $query->set( 'post_type', array(
           'post', 'specials'
          ));
       }
    }
    add_action( 'pre_get_posts', 'special_add_custom_types' );

    The above together with the top bit of code makes it fully functional!

    One last question, is it possible to make the post default to checking the category daily-specials so the user does not need to check the box?

    Thanks

    • This reply was modified 10 months, 2 weeks ago by Hisham.

    Great to hear that you’ve managed to integrate the custom post type into your category and tag archives with that bit of code! It’s always satisfying when things start falling into place.

    Regarding your question about defaulting the post to a specific category like ‘daily-specials’, yes, it’s definitely possible in WordPress. You can achieve this by adding a function to your child theme’s functions.php file. This function will automatically check the ‘daily-specials’ category whenever you create a new post. Here’s how you can do it:

    function auto_select_daily_specials_category( $post_ID ) { $daily_specials_cat = get_term_by('slug', 'daily-specials', 'category'); if ( !has_term( 'daily-specials', 'category', $post_ID ) ) { wp_set_post_categories( $post_ID, array( $daily_specials_cat->term_id ), true ); } } add_action( 'save_post', 'auto_select_daily_specials_category' );

    Please note:

    • Replace 'daily-specials' with the actual slug of your category if it’s different.
    • This will add the ‘daily-specials’ category to all new posts, so if you have different post types and you only want this to apply to a specific type (like ‘specials’), you might need to modify the function to check the post type first.

    By adding this function, you’re streamlining the process for users, making it more convenient and reducing the chances of forgetting to categorize a post correctly. It’s these little tweaks that can really enhance the user experience on your WordPress site!

    Thread Starter Hisham

    (@ctrlsync)

    That last bit of code did the trick! Thanks so much for all the help it is very much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom post category not working’ is closed to new replies.