Site logo

[Resolved] Change post per page – custom post type archive page. Element templates skipped

Home Forums Support [Resolved] Change post per page – custom post type archive page. Element templates skipped

Home Forums Support Change post per page – custom post type archive page. Element templates skipped

Viewing 15 posts - 1 through 15 (of 35 total)
  • Author
    Posts
  • #2103368
    Brad

    Create CPT named Photography.
    Trying to limit / change number of posts on archive page to 3 ( for testing )

    Code below works but skips over all the GenerateBlocks customization; looks like it defaults back to archive.php without using GP elements – header, elements – block – content template. Etc.

    Note: in child theme I did create archive-photography.php, content-photography.php, single-photography.php

    Found this code from another GP support thread and amended it to target archive post type.

    https://generatepress.com/forums/topic/limit-the-number-of-post/

    function j66co_photography_posts_per_page($query) {
    if (is_post_type_archive( ‘photography’ )) {
    $query->set(‘posts_per_page’, 3);
    }
    }

    add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page’);

    Thoughts? Thank for help in advance.

    #2103500
    Elvin
    Staff
    Customer Support

    Hi Brad,

    Try adding this snippet:

    add_filter( 'generate_elements_custom_args', function( $args ) {
    
        $args['suppress_filters'] = true;
    
        return $args;
    
    } );

    This should make the Block Element – Content Template work with pre_get_posts.

    #2103536
    Brad

    No. Didn’t work. I’m using code snippet plugin right now for testing. It’s set at priority 10. Both together as follows:

    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’ )) {
    $query->set(‘posts_per_page’, 3);
    }
    }

    add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page’);

    #2103557
    Elvin
    Staff
    Customer Support

    Strange. I’ve used the exact code and it seems to be working as you can see on this loose replication.
    http://elvin.wppluginsupport.net/photography

    This sandbox site is using the same code you’ve shared as shown here –
    https://share.getcloudapp.com/BludboLe

    If you check the page, the Block content template is applied AND there are only 3 posts per page on the archive.

    Perhaps there are other codes or plugins causing conflicts.

    Can you let us have temporary backend access to take a look? You can use the private information text field for this –
    https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2103565
    Brad

    Just saw reply. Create admin user. Info in private box. Thanks for taking a look.

    #2103568
    Elvin
    Staff
    Customer Support

    Ah. I missed one important context.

    I see that you have a child theme. Can you disable the child theme and see if it works?

    The child theme’s template is likely overriding the effects of the Block element – content template as child theme’s template serve the same purpose as what Block element – Content template does.

    #2104452
    Brad

    Just seeing this child theme reply. Saw you login last night. Did you have any luck? Want me to disable child theme? I’m afraid that will break the whole site.

    #2104585
    Elvin
    Staff
    Customer Support

    Try to at least remove archive-photography.php as that’s most likely the specific one that may have caused the issue.

    #2104604
    Brad

    No. Renamed it. Didn’t work.

    #2104650
    Elvin
    Staff
    Customer Support

    Just seeing this child theme reply. Saw you login last night. Did you have any luck? Want me to disable child theme? I’m afraid that will break the whole site.

    In this context, can we clone the site to a staging site? So we can disable a few things without worry about the live site breaking.

    We’ll likely have to disable the child theme and plugins and enable them one by one to be sure which one is causing the issue.

    #2104689
    Brad

    Sure. It’s my dev server domain. I’ve been up since 3a.,it’s 9p now. Really tired. I’ll hit it up tomorrow and comment back. Next couple days / whenever you have a chance to take a look a staging that would be cool.

    I’ll deactivate plug-ins one by one on staging after creating staging first and comment back. Just give me a day or so.

    #2104690
    Elvin
    Staff
    Customer Support

    Copy. You can bump this thread to let us know when it’s up. 🙂

    #2115836
    Brad

    Hey Elvin / Team GeneratePress

    I finally got a chance to clone my dev site to staging. Login credentials are in private section if still open to taking a look.

    Background
    As suggested in your last comment from a few days ago, I deactivated the child theme. No luck solving problems though.

    ___________

    Issue:

    Trying to reduce number of posts from default 10 to 3 for testing in the “photography” CPT (and eventually “showcase” CPT).

    ____________

    Result:

    a.
    It works but all Generateblocks Elements Header Hero images / Content Template layouts get disregarding.

    b.
    Pagination stops working. Page 2.. opps not found.

    c.
    Mobile menu stopped working on pages with var dump code.

    Code:

    function j66co_photography_posts_per_page($query) {
    if (is_post_type_archive( ‘photography’ )) {
    $query->set(‘posts_per_page’, 3);
    }
    }

    add_action(‘pre_get_posts’, ‘j66co_photography_posts_per_page’);

    ____________

    What’s set in GB Elements?

    Locations to display the Content Templates / Headers are all set to the correct targets (all category archives) etc.

    A couple of elements won’t allow drop down; hence I tried the filter below. Problem. Couldn’t located ID of category archive / CPT archive pages. Amended var dump code into archive-photography.php / archive-showcase.php

    int(0) returned

    Code:

    $posts = get_posts( );
    echo ‘

    ';
    		var_dump(get_queried_object_id());
    	echo '

    ‘;
    die( );

    ////////////////

    Issue:

    Trying to have CPT show up in categories archive.

    Result:

    Same as above. Got it to work but it’s not using the (a.) The Generateblocks Elements Header Hero images / Content Template layouts get disregarding.

    Code:

    // show CPT in Categories
    // https://wpbeaches.com/show-custom-post-types-category-archive-page/

    function j66co_show_cpt_archives( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $query->set( ‘post_type’, array(
    ‘post’, ‘nav_menu_item’, ‘showcase’
    ));
    return $query;
    }
    }
    add_filter( ‘pre_get_posts’, ‘j66co_show_cpt_archives’ );

    ////////////////

    Thanks for your assistance in helping me unwind this. I do sincerely appreciate it.

    Brad

    #2117273
    Elvin
    Staff
    Customer Support

    Can you keep the child themes and ALL plugins deactivated(except GP Premium and CPT UI)?

    So we can find a base/control version where everything works and we’re sure it has no conflict. (similar to the working example here – http://elvin.wppluginsupport.net/photography)

    We’re close. We just need to figure out what’s messing w/ the code as I’m sure it works. The site I’ve linked actually uses the exact code your site uses – https://share.getcloudapp.com/Apuxo7kL

    If you have multiple snippets w/ different instances of pre_get_posts, I think we can re-write them all into just 1 filter instead of having multiple instances. (I think you have 2 on the code snippets)

    #2117313
    Brad

    Sure. Sincere thanks for taking a look. Feel free to do whatever you want to the staging site!

    Deactivate plugins, etc. I’m away from desktop right now on iphone.

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