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 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #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/

    https://www.thirtypointfour.com.au/code/filtering-results-on-an-archive-or-taxonomy-page-with-pre_get_posts/

    https://wpshout.com/practical-uses-pre_get_posts/

    #2130171
    Elvin
    Staff
    Customer Support

    Hi 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') );
        }
    }
    #2134990
    Brad

    Ok. I’m back to this again.

    Objective 1:
    Limit posts to 6 across the site.

    Status:
    Resolved

    I 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-archives

    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’ );

    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

    #2135929
    Elvin
    Staff
    Customer Support

    Do 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. 🙂

    #2135996
    Brad

    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.

    #2136000
    Brad

    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

    #2136088
    Elvin
    Staff
    Customer Support

    Your 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') );
        }
    #2136768
    Brad

    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

    #2137083
    Brad

    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);

    #2137091
    Brad

    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);

    #2137204
    Elvin
    Staff
    Customer Support

    Question:
    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);
    #2137356
    Brad

    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: 4

    Objective 2
    Have cpt display in categories and tags archives

    Snippets areas are individual so they can be turned on and off. Thoughts?

    #2137360
    Brad

    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);

    #2137377
    Elvin
    Staff
    Customer Support

    See 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.

    #2137438
    Brad

    This “condensed code” should work but it’s not.

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