Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Use archive template to create custom wp_query combine posts, pages & cpt

  • smarterwebpackages

    I want to create an archive template by copying the default GP archive.php file and adding PHP code to allow me to include the display of cpt (single or multiple) along with regular posts and pages.

    I understand basic php and I just need help to modify existing code and get pointed towards the right direction. For example, in the archive.php file, the code below, what function code should I add in order to override the “generate_has_default_loop” in order for me to combine cpt, post & pages turning it into one combined archive. Or feel free to give me your approach.

    if ( generate_has_default_loop() ) {
    	if ( have_posts() ) :
    		while ( have_posts() ) :
    			the_post();
    		endwhile;
    	else :
    	endif;
    }

    Note that I’m familiar with GP hooks, layouts and structures and will maintain that part. I just don’t know how to add functions on WP_Query to modify the default loop. Thank you for your help!

  • smarterwebpackages

    Use case scenario 1:
    I’m using the template to create an archive that would combine “ALL” posts (including post, page and cpt). I’m familiar with how GP renders the content – I just need the code that allows me to modify the main query (whether it’s on the template page or the functions.php).

    Use case scenario 2:
    I’m going to use the php snippet to create a shortcode that I can show in gutenberg simply display a list of “ALL” posts (including post, page and cpt) depending on the condition I set.

    Both case scenarios are similar in it’s objective which is to modify the wp_query and display the results in a loop – whether that be in an template file or to be used as a shortcode. I hope this gives you some context on how I intend on using this.

  • Hi there,

    You can use a GB query loop block on a static page, and use this snippet to include other post types.
    https://docs.generateblocks.com/article/add-multiple-post-types-in-one-query-loop/

  • smarterwebpackages

    I’m familiar with the query block and I will use the version you recommended. That is a powerful tool but can also have limitations. I would still appreciate an example of what the PHP code would look like if I added it into (copy of the archive.php) the template page designed specifically to display posts loop.

    I know I’ll eventually figure out it by Googling and getting a sense of past suggestions like: https://generatepress.com/forums/topic/removing-default-loop-from-pages/

    But I would appreciate any more suggestions or actual PHP code you can share to nudge me towards the right direction.

  • Say we’ve created this template php file, how do you plan to assign it to a page? As WP does not generate such an archive.

  • smarterwebpackages

    You’ve made an excellent point. I’m probably trying to create something that might only exist in my head. I suppose a good approach is to go back to the drawing board, try to see if GP Query loop block will be sufficient or at least visually figure out the final outcome of what I’m trying to build in the first place. I’m gonna get back to this post later on and post what I create.

    If my goal is to create WordPress archive for displaying posts including post, page & cpt together in a single loop, I know I can do the following:

    Use custom taxonomy (or tags / categories) as a means to combine multiple posts and then use the wp archive template for that taxonomy to create conditions on how it would display it’s content (ex: video cpt, audio cpt). I’ve already managed to create an taxonomy-X archive designated to show different content-X layouts by creating conditional functions.

    For now, I’ll try to figure out what I’m trying to accomplish in the first place.

  • Use custom taxonomy (or tags / categories) as a means to combine multiple posts and then use the wp archive template for that taxonomy to create conditions on how it would display it’s content (ex: video cpt, audio cpt). I’ve already managed to create an taxonomy-X archive designated to show different content-X layouts by creating conditional functions.

    Yes, that seems a way 🙂

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