Site logo

Archived topic

Change post per page - custom post type archive page. Element templates skipped

34 replies · Started by Brad on February 2, 2022

Viewing posts 1–15 of 35

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.

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.

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

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

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

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.

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.

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

No. Renamed it. Didn’t work.

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.

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.

Copy. You can bump this thread to let us know when it's up. :)

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

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)

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.

This archived topic is closed to new replies.