Site logo

[Resolved] Make cpt photography assigned taxonomies display correctly GB – pre_get_posts

Home Forums Support [Resolved] Make cpt photography assigned taxonomies display correctly GB – pre_get_posts

Home Forums Support Make cpt photography assigned taxonomies display correctly GB – pre_get_posts

Viewing 6 posts - 16 through 21 (of 21 total)
  • Author
    Posts
  • #2137450
    Elvin
    Staff
    Customer Support

    Ah yeah that’s right. I’ve used the wrong callback.

    The add_action should’ve used j66co_posts_per_page_and_post_type.

    I’ve corrected the code. Can you test again?

    #2137539
    Brad

    Just saw this. On mobile . Will try once i get to iPad or desktop. Thanks for reply

    #2137551
    Brad

    Ok. It worked!! Your condensed code worked. Sincere thanks. What’s interesting is I’ve done nothing to add support to display CPT in category or tags and they are showing up once we add the snippet which just added.

    I do have the following which I can enable in a separate code snippet:

    function my_cptui_add_post_types_to_archives( $query ) {
    // We do not want unintended consequences.
    if ( is_admin() || ! $query->is_main_query() ) {
    return;
    }

    if ( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );

    Thoughts?

    #2137564
    Elvin
    Staff
    Customer Support

    I’m not exactly sure what cptui_get_post_type_slugs(); but if it does what I think it does,(return an array of all slugs of post types created within CPT UI), then you can merge it on to the condensed code.

    Like this:

    add_filter( 'generate_elements_custom_args', function( $args ) {
        $args['suppress_filters'] = true;
        return $args;
    } );
    
    function j66co_posts_per_page_and_post_type($query) {
        //Post per page – CPT showcase: 4
        if(! is_admin() && $query->is_main_query()){
            if ( is_post_type_archive( 'showcase' ) && empty( $query->query_vars['suppress_filters'] ) ) {
                $query->set('posts_per_page', 4);
            }
            //Post per page (cpt photography, categories, tags): 6
            if ( ( is_category() || is_tag() ||  is_post_type_archive( 'photography' ) ) && empty( $query->query_vars['suppress_filters'] ) ) {
                $query->set('posts_per_page', 6);
            }
            //Have the default post type "post" and CPTs "showcase" and "photography" display in categories and tags archives
            if( ( is_category() || is_tag() ) && empty( $query->query_vars['suppress_filters'] ) ) {
                $cptui_post_types = cptui_get_post_type_slugs();
                
                $query->set( 'post_type', array_merge( array('post') , $cptui_post_types ) );
            }
        }
        return $query;
    }
    add_action('pre_get_posts', 'j66co_posts_per_page_and_post_type', 99, 1);
    #2137587
    Brad

    Cool. Sincere thanks man for all your patience and help. I really appreciate it. It would if taken me 2 more months at least to build this site.

    Where’d you’d learn your skills. Computer science major in college? Tech boot camp? Self taught? (If you don’t mind me asking?

    I have a web certificate that’s ancient and everything else is self taught.

    #2138738
    Elvin
    Staff
    Customer Support

    No problem. 😀

    Where’d you’d learn your skills. Computer science major in college? Tech boot camp? Self taught? (If you don’t mind me asking?

    I didn’t have any formal education from any institution for WordPress. Mostly self-taught but I think it comes naturally as all of my siblings know their way around WordPress as well.

    TBH, I just read the manual and the code itself. Everything else is grasping concepts and having creativity+resourcefulness in executing the concept you’ve grasped. 😀

Viewing 6 posts - 16 through 21 (of 21 total)
  • You must be logged in to reply to this topic.