[Support request] Display posts from a taxonomy & a specific term from a custom taxonomy

Home Forums Support Display posts from a taxonomy & a specific term from a custom taxonomy

  • This topic has 17 replies, 3 voices, and was last updated 1 year ago by Tom.
Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #1013395
    Créateur

    We created a custom taxonomy called “issue“. In this taxonomy “issue”, we create terms like “Issues by date”. For example, “Issue from 19.09.2019”. We will be able to know in which issue the post we publish was published. The post are from a weekly newspaper, that’s the reason why we need to know in which issue the post was published.

    We also have to display the issues from the custom taxonomy “issue” and a specific category in a new archive template. For that, I created a file taxonomy-issue.php and uploaded to child theme folder. Now I am trying to display all posts from “Issue from 19.09.2019” sorted by category “Economy” with WP Show Posts Pro with the function like:

    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38803 ); ?>

    The problem is that we cannot choose a taxonomy “economy” and a term from custom taxonomy “Issue from 19.09.2019” to display only posts from the issue “Issue from 19.09.2019” and the category “economy …

    I cannot see the forest for the trees. Could please give me hint how to solve this. Is this even possible with WP Show Post Pro?

    #1013427
    Créateur

    It is possible to display WordPress filter functions by URL manipulation, like:

    https://example.org/issue/issue-from-19-09-2019/?category_name=economy
    or
    https://example.org/?category_name=economy&issue=issue-from-19-09-2019
    or
    https://example.org/?category_name=wordpress&tag=plugins

    Any change to filter like this in WP Show Posts Pro? Or as a function?

    Thank you.

    #1013428
    mikeb82

    Bill Erickson’s Display Posts plugin has a lot of options.
    https://wordpress.org/plugins/display-posts-shortcode/

    #1013710
    Créateur

    @mikeb82 THX

    But we are working WP Show Posts Pro. Would be nice to filter like this in WP Show Posts Pro, too?
    https://example.org/?category_name=wordpress&tag=plugins

    #1013721
    Créateur

    Therefore, in WP Show Post it should be possible to select 2 times in a taxonomy?
    https://prnt.sc/p8bgp0

    #1013864
    Tom
    Lead Developer
    Lead Developer

    As of right now, it’s not possible to specify multiple taxonomies, unfortunately.

    What you can try is adding $settings to this line: https://github.com/tomusborne/wp-show-posts/blob/1.1.3/wp-show-posts.php#L383

    So it looks like this: https://github.com/tomusborne/wp-show-posts/blob/release/1.2/wp-show-posts.php#L391

    That will be in WPSP 1.2.

    Then you can try something like this:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 123 === $settings['list_id'] ) {
            $tax_args = array(
                'tax_query' => array(
                    'relation' => 'AND',
    		array(
                        'taxonomy' => 'issue'
                        'field'    => 'term_id',
                        'terms'    => 100
                    ),
                    array(
                        'taxonomy' => 'acme_labels',
                        'field'    => 'term_id',
                        'terms'    => 200
                    )
    	    )
            );
    
            $args = array_merge( $args, $tax_args );
        }
    
        return $args;
    }, 10, 2 );

    This article should help when it comes to actually filling out the fields above: https://tommcfarlin.com/get-posts-with-multiple-taxonomies/

    You also need to replace 123 with the ID of the list you’re targeting.

    #1014304
    Créateur

    Thank you so much! This works perfectly.

    However, I obviously had the wrong approach to our requirement. With this approach we would have to create at least one new snippet (as above) and lots of WPSP lists for each output.

    Our goal is to output a certain issue (about 50 news items) — structured by category – in an archive template.

    Do you have any ideas how best to solve the requirement?

    I absolutely wanted it to work with WPSP, apparently it doesn’t work as I wanted …

    #1014679
    Tom
    Lead Developer
    Lead Developer

    Hmm, I can’t think of a way to do it automatically like that.

    Do you have any examples of the page you’re trying to build?

    #1015127
    Créateur

    I just posted an URL in the edit Post area.

    This link directly opens an Issue in an archive template.

    We are trying to output the single post of the issue structured by category — in an archive template.

    <?php
    /**
     * The template for displaying Archive pages.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
    <h3 class="widget-title">Economy</h3>
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38852 ); ?>
    
    <h3 class="widget-title">Politics</h3>
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38852 ); ?>
    
    <h3 class="widget-title">Culture</h3>
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38852 ); ?>
    
    <h3 class="widget-title">Sports</h3>
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38852 ); ?>
    
    <h3 class="widget-title">ETC</h3>
    
    <?php if ( function_exists( 'wpsp_display' ) ) wpsp_display( 38852 ); ?>
    
    	
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    get_footer();
    
    #1015598
    Tom
    Lead Developer
    Lead Developer

    Hmm, I can’t think of a way to automatically do it. If you can find a tutorial (I’m sure one exists) on how to do with a custom loop, I may be able to help figure out how to do it inside WPSP.

    #1015695
    Créateur

    This might be a possible solution?

    https://hookagency.com/display-posts-in-archives-pages/#modify-theme

    The code queries the correct posts from the CPT “issue” but from all categories and not only a specific category.

    <div class="posts-wrapper">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php if(is_category('featured')): ?>class="featured-post"<?php endif; ?>>
    <h1 class="post-title"><?php the_title() ;?></h1>		
    <p class="date-and-category">
    Published on <?php the_time('M j, Y'); ?> 
    by <?php the_category(', '); ?>
    in <?php the_category(', '); ?>
    </p>
    <div class="post-content"><?php the_content(); ?></div>
    <div class="prev-next-links">
    <ul>
    <li class="next-post"><?php next_post_link(); ?></li>
    <li class="prev-post"><?php previous_post_link(); ?></li>
    </ul>
    </div>
    </article>
    <?php endwhile; else: ?>
    <p>Sorry, this post does not exist</p>
    <?php endif; ?>
    </div>

    https://wordpress.stackexchange.com/a/155976/31545

    #1015698
    Créateur
    #1015711
    Créateur

    Under the website URL (only admins can see) you can see now that all posts and categories are listed from the correct issue.

    <div class="posts-wrapper">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <article id="post-<?php the_ID(); ?>" <?php if(is_category('featured')): ?>class="featured-post"<?php endif; ?>>
    	<h3 class="post-title"><?php the_title() ;?></h3>		
    
    	<p class="date-and-category">
    		Published on <?php the_time('M j, Y'); ?> 
    		by <?php the_author(', '); ?>
    		in <?php the_category(', '); ?>
    	</p>
    
    </article>
    
    <?php endwhile; else: ?>
    
    	<p>Sorry, this post does not exist</p>
    
    <?php endif; ?>
    </div>

    So is it possible to query categories one by one to be able to add dividers as a headline?

    <h3 class="widget-title">Category Economy</h3>

    – all posts from category Economy

    #1016013
    Tom
    Lead Developer
    Lead Developer

    You would need to create a bunch of different queries, each one grabbing posts from a different category.

    You can learn more about queries here: https://www.smashingmagazine.com/2013/01/using-wp_query-wordpress/

    This tool might be helpful: https://generatewp.com/wp_query/

    #1016926
    Créateur

    Thank you very much so far!

    I am not a pro, probably I will need some time to figure out to set queries, each one grabbing posts from a different category.

    Hope you than help to figure out how to do it inside WPSP like mentioned before.

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