Home › Forums › Support › Make cpt photography assigned taxonomies display correctly GB – pre_get_posts
- This topic has 20 replies, 2 voices, and was last updated 4 years, 1 month ago by
Elvin.
-
AuthorPosts
-
February 23, 2022 at 2:19 am #2130162
Brad
Trying to figure this out. Had issue two weeks ago with different cpt. Got it resolved with your guys / gals help through deleting scripts and reducing elements display rules. This one with cpt “Photography” remains though. Here’s the code I’ve hacked together in my attempt. Any thoughts? Sincere thanks for your assistance.
Good news. This is my last main structural challenge on the site before I launch version 1.0 Mon Feb28.
________________________
add_action( ‘pre_get_posts’, ‘target_taxonomy_of_photography_query_with_conditional’ );
function target_taxonomy_of_photography_query_with_conditional ( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It’s the main query for a front end page of your site.if ( is_tax()
// It’s the main query for a taxonomy archive.
&&
empty( $query->query_vars[‘suppress_filters’] )
}
$query->set( ‘post_type’, array(
‘post’,
‘photography’
) );
}
}________________________
Site link
CPT “photography” — taxonomy acting as a category -> iphone_pro -> new iphone_ pro -> macro
https://blocks-01.j66labs.co/photography/page/2/
https://blocks-01.j66labs.co/iphone_pro/macro/
CPT “photography” — tag create in CPT photography -> “farmers market”
https://blocks-01.j66labs.co/tag/farmers-market/
________________________
Research / reference links
https://developer.wordpress.org/reference/hooks/pre_get_posts/
February 23, 2022 at 2:31 am #2130171Elvin
StaffCustomer SupportHi Brad,
Try this:
add_action( 'pre_get_posts', 'target_taxonomy_of_photography_query_with_conditional' ); function target_taxonomy_of_photography_query_with_conditional ( $query ) { if ( ! is_admin() && $query->is_tax() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'photography') ); } }February 26, 2022 at 5:51 pm #2134990Brad
Ok. I’m back to this again.
Objective 1:
Limit posts to 6 across the site.Status:
ResolvedI have the following code in section two below (which i know is not written well; I’m not sure how to write arrays).
This limits the number of posts to 6 basically across the site.What’s interesting is somehow this from section two code
add_filter( ‘generate_elements_custom_args’, function( $args ) {
$args[‘suppress_filters’] = true;
return $args;
} );…with pre_get_posts
Allows the CPT of “showcase” to appear in the archive of the wp core categories and tags.
It does not though work automatically for the CPT “photography”.Thus, I tried adding this code snippet. It unfortunately didn’t work.
“How to add your new post type to WordPress category and tag archives”
https://docs.pluginize.com/article/17-post-types-in-category-tag-archivesfunction 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’ );Any suggestion from here??
______________________________________________________
______________________________________________________add_filter( ‘generate_elements_custom_args’, function( $args ) {
$args[‘suppress_filters’] = true;
return $args;
} );function j66co_photography_posts_per_page($query) {
if ( is_post_type_archive( ‘photography’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’photography’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page’, 99, 1);function j66co_showcase_posts_per_page($query) {
if ( is_post_type_archive( ‘showcase’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’showcase’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_showcase_posts_per_page’, 99, 1);// trying new code category
function j66co_posts_per_page_categories($query) {
if ( is_category() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’showcase’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_posts_per_page_categories’, 99, 1);// trying new code taxonomies
function j66co_posts_per_page_tag($query) {
if ( is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’showcase’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_posts_per_page_tag’, 99, 1);______________________________________________________
______________________________________________________I put links to new slimmed down staging site in private area
February 27, 2022 at 5:18 pm #2135929Elvin
StaffCustomer SupportDo you have all of these codes active in one site? If yes, it can potentially mess the queries up.
We should just maintain one and edit it to what is needed.
As for the issue:
I’m not sure what the issue is. Can you clarify?
If you’re trying to display “showcase” on a “photography” CPT archive, I’m not sure that’s a good idea since the whole point of having photography post archive is to exclusively display posts under photography. Having “showcase” in it defeats the purpose of a “photography” cpt. đ
I suggest reconsidering the information structure of your site. Based from this, it sounded like, you should’ve had “photography” as a custom taxonomy instead of a post type. đ
February 27, 2022 at 6:58 pm #2135996Brad
The coding issue is I donât know how to write an array.
It makes sense what youâre saying regarding potential conflicts.
Both showcase and photography are CPT.
The issue
Custom taxonomies assigned to CPT.The site structure strategy I already change once. It was a major change as I ditched all CPT taxonomies because of âthe issueâ and opted to just use Wp core category and tags.
So the problem that remains is on the CPT photography for some reason itâs not allowing itself to be displayed in Wp core taxonomies archive results.
Hopefully this makes sense.
February 27, 2022 at 7:05 pm #2136000Brad
Note: Iâm really try to keep showcase CPT snd photography CPT separate. The first is work (web apps, design, creative campaigns, strategy consulting, product development), the second is just my for my enjoyment (photography as a creative outlet). I donât offer photographic services. I donât consider myself a commercial photographer.
Hence, this was my reasoning for creating two separate, independent CPTs
February 27, 2022 at 9:56 pm #2136088Elvin
StaffCustomer SupportYour pre_get_posts are conflicting.
as previously mentioned on this thread on ALL of your other threads, we really should just maintain one.
I’m not saying this just for the purpose of being tidy. It is actually the one causing the issues you’re having.
As for the issue itself:
These two –
if( ( is_category() || is_tag() ) && empty( $query->query_vars[âsuppress_filtersâ] ) ) { $query->set( âpost_typeâ, array( âpostâ,âphotographyâ) ); return $query; }and
if( ( is_category() || is_tag() ) && empty( $query->query_vars[âsuppress_filtersâ] ) ) { $query->set( âpost_typeâ, array( âpostâ,âshowcaseâ) ); return $query; }– messes the query up. The first one is being modified by the second one because they have the exact same condition.
It should be:
if ( ( is_category() || is_tag() ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'photography', 'showcase') ); }February 28, 2022 at 8:02 am #2136768Brad
I have two snippets.
Snippet 1 – limit number of post to 8 for CPT showcase, photography, Wp core categories, Wp core tags.
It’s four function in one snippet area. I don’t know how to write it as an if statement or array.Snippet 2 – display CPT with Wp core categories and tag / taxonomies
This is just from a tutorial_______
I appreciate your time, assistance; you sharing your insight. I am applying your advice as best as possible.
Self taught coder starting way back in 1999 with actionscript 1.0 believe it or not. Tell target movie script. They didn’t use dot notation until a Actionscript 2.0. I attempted to teach myself OOP from a Flash Actionscript perspective. The theory I understand. Writing it though, whether it be Actionscript or a php is another story.
Functions (Wp) I can write single servings looking things up in the codex. That’s part of the problem here. I did a copy paste of my function, change the function name and CPT accordingly. All four “limit posts to 8” are in one snippet area. I can turn it off and on at anytime.
Question:
It sounds like you can only have pre_get_posts once. Is this correct?I have it four times in one snippet area (four functions).
I don’t know how to write this as a series of if statement or as an array?? (Not sure if that’s what I need). I’ve got a question in an intermediate Wp fb group right now. Hopefully I’ll get some feedback. I’ve been searching google, stackexchange as well.
________
I’ve got two basic objectives.
I’ve got two snippets
(One snippet has four single functions in it, with pre_get_post four times)OBJECTIVE 1 –
Limit number of posts to 8 basically for the whole site.OBJECTIVE 2 –
Allow CPT to display / show up is Wp core taxonomies / category archive results as well as have them appear in their own assigned taxonomies archive page.PROBLEM / ISSUE
With just adding the pre_get_posts the child theme plug-in customization gets ignored. Adding the following sometimes corrects this.add_filter( âgenerate_elements_custom_argsâ, function( $args ) {
$args[âsuppress_filtersâ] = true;
return $args;
} );____________
Iâll duplicate my snippet.
Turn off the âolder one. name it 220228..name the new on 220301..
Try to code the suggestions above you mention
February 28, 2022 at 1:27 pm #2137083Brad
OK. This is working. I condensed the array. thank you for showing me how to write that. I wish there was a way to do that for the rest of the code. Iâm using per_get_posts many times and know where am I having to add the code to display CPT with wp core categories and tags; itâs just happening on its own. Not sure why.
All this is in one snippet. Want to condense it more? See anything?
_________________________
add_filter( ‘generate_elements_custom_args’, function( $args ) {
$args[‘suppress_filters’] = true;
return $args;
} );function j66co_photography_posts_per_page($query) {
if ( is_post_type_archive( ‘photography’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’photography’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page’, 99, 1);function j66co_showcase_posts_per_page($query) {
if ( is_post_type_archive( ‘showcase’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’showcase’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_showcase_posts_per_page’, 99, 1);// trying new code category – showcase and photography 220228
function j66co_posts_per_page_categories($query) {
if ( is_category() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’, ‘showcase’, ‘photography’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_posts_per_page_categories’, 99, 1);// trying new code taxonomies – showcase and photography 220228
function j66co_posts_per_page_tag($query) {
if ( is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’, ‘showcase’, ‘photography’) );return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_posts_per_page_tag’, 99, 1);February 28, 2022 at 1:39 pm #2137091Brad
Trying this as a test as well..
// https://developer.wordpress.org/reference/hooks/pre_get_posts/
function j66co_photography_posts_per_page_is_main_query($query) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It’s the main query for a front end page of your site.if ( is_post_type_archive( ‘photography’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 2); // TESTING TO SEE IF IT WORKS
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’,’photography’) );return $query;
}
}}
add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page_is_main_query’, 99, 1);February 28, 2022 at 5:08 pm #2137204Elvin
StaffCustomer SupportQuestion:
It sounds like you can only have pre_get_posts once. Is this correct?You can have multiple pre_get_posts. đ You can even have multiple functions hooked into it.
The issue is you’re using the exact same condition multiple times. It’s overriding itself when you do that. đ
I think you can just compress everything into this:
add_filter( 'generate_elements_custom_args', function( $args ) { $args['suppress_filters'] = true; return $args; } ); function j66co_posts_per_page_and_post_type($query) { if ( ( is_category() || is_tag() || is_post_type_archive( 'showcase' ) || is_post_type_archive( 'photography' ) ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set('posts_per_page', 6); } if( ( is_category() || is_tag() ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'showcase', 'photography') ); } return $query; } add_action('pre_get_posts', 'j66co_posts_per_page_categories', 99, 1);February 28, 2022 at 10:24 pm #2137356Brad
I condense the code. As written it makes sense. Thank you for composing it.
Unfortunately it doesnât work. It doesnât limit the post per page on any cpt.
If I leave the code long winded written out as single serving functions, it does work to limit the posts per page. As a bonus, CPT are displaying in Wp core categories and tags. It shouldnât work but it does.
Objective 1
Post per page (cpt photography, categories, tags): 6
Post per page – CPT showcase: 4Objective 2
Have cpt display in categories and tags archivesSnippets areas are individual so they can be turned on and off. Thoughts?
February 28, 2022 at 10:32 pm #2137360Brad
Updated. I think this is correct. Itâs not going to work though
add_filter( ‘generate_elements_custom_args’, function( $args ) {
$args[‘suppress_filters’] = true;
return $args;
} );function j66co_posts_per_page_and_post_type($query) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It’s the main query for a front end page of your site.if ( ( is_category() || is_tag() || is_post_type_archive( ‘photography’ ) ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 6);
}
if ( ( is_category() || is_tag() || is_post_type_archive( ‘showcase’ ) ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set(‘posts_per_page’, 4);
}if( ( is_category() || is_tag() ) && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array( ‘post’, ‘showcase’, ‘photography’) );
}
return $query;
}
}
add_action(‘pre_get_posts’, ‘j66co_posts_per_page_categories’, 99, 1);February 28, 2022 at 11:05 pm #2137377Elvin
StaffCustomer SupportSee 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'] ) ) { $query->set( 'post_type', array( 'post', 'showcase', 'photography') ); } } return $query; } add_action('pre_get_posts', 'j66co_posts_per_page_and_post_type', 99, 1);I left comments before the if statement to note what they do specifically.
March 1, 2022 at 12:10 am #2137438Brad
This âcondensed codeâ should work but itâs not.
-
AuthorPosts
- You must be logged in to reply to this topic.