Archived topic
Make cpt photography assigned taxonomies display correctly GB - pre_get_posts
20 replies · Started by Brad on February 23, 2022
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?
Just saw this. On mobile . Will try once i get to iPad or desktop. Thanks for reply
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?
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);
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.
No problem. :D
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. :D