categories - Get Posts by Category, Tag , and CPT Taxonomy

admin2025-03-11  3

How to print only those posts which have Category, Tag, CPT Taxonomy.

I have tried out with this code but nothing prints. To print posts I used SS [commonposts category="2" tag="3" taxonomy="company" tax_term="4" posts_per_page="5"]

function common_cats($att){
    $args = array(
    'post_type'=> 'post',
    'category'    => ['category'],
    'tag'    => ['tag'],
    'taxonomy'    => ['taxonomy'],
    'tax_term'    => ['tax_term'],
    'posts_per_page' => ['posts_per_page']
     );              
  $the_query = new WP_Query( $args );
   if($the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : 
     $the_query->the_post(); 
      $output .= "<a href=".get_permalink().">.the_title().</a>";
  endwhile; 
    wp_reset_postdata(); 
else: 
endif;
}
add_shortcode('commonposts', 'common_cats');

Appreciate everybody's effort.

How to print only those posts which have Category, Tag, CPT Taxonomy.

I have tried out with this code but nothing prints. To print posts I used SS [commonposts category="2" tag="3" taxonomy="company" tax_term="4" posts_per_page="5"]

function common_cats($att){
    $args = array(
    'post_type'=> 'post',
    'category'    => ['category'],
    'tag'    => ['tag'],
    'taxonomy'    => ['taxonomy'],
    'tax_term'    => ['tax_term'],
    'posts_per_page' => ['posts_per_page']
     );              
  $the_query = new WP_Query( $args );
   if($the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : 
     $the_query->the_post(); 
      $output .= "<a href=".get_permalink().">.the_title().</a>";
  endwhile; 
    wp_reset_postdata(); 
else: 
endif;
}
add_shortcode('commonposts', 'common_cats');

Appreciate everybody's effort.

Share Improve this question asked Mar 14, 2021 at 10:08 PuneetPuneet 477 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It looks like you're not passing the shortcode attributes to the query arguments at all. Also the arguments array structure a bit wonky. The WP_Query parameter docs is your friend when creating a custom query. Your shortcode should also return its output to show anything.

Here's a modified version of your code that should point you in the right direction.

function common_cats($attributes){

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => ! empty($attributes['posts_per_page']) ? absint($attributes['posts_per_page']) : 5,
    );

    /**
      * Category parameters
      * @source https://developer.wordpress/reference/classes/wp_query/#category-parameters
      */
    if ( ! empty($attributes['category']) ) {
        if ( is_numeric($attributes['category']) ) {
            // Category ID
            $args['cat'] = absint($attributes['category']);
        } else {
            // Category slug
            $args['category_name'] = $attributes['category'];
        }
    }

    /**
      * Tag parameters
      * @source https://developer.wordpress/reference/classes/wp_query/#tag-parameters
      */
    if ( ! empty($attributes['tag']) ) {
        if ( is_numeric($attributes['tag']) ) {
            // Tag ID
            $args['tag_id'] = absint($attributes['tag']);
        } else {
            // Tag slug
            $args['tag'] = $attributes['tag'];
        }
    }

    /**
      * Custom taxonomy parameters
      * @source https://developer.wordpress/reference/classes/wp_query/#taxonomy-parameters
      */
    if (
        ! empty($attributes['taxonomy']) &&
        ! empty($attributes['tax_term'])
    ) {
        $args['term_query'] = array(
            array(
                'taxonomy' => $attributes['taxonomy'],
                'field' => 'term_id',
                'terms' => absint($attributes['tax_term']),
            ),
        );
    }

    // helper variable to hold the shortcode output
    $output = '';

    // query posts
    $query = new WP_Query( $args );

    // You can check for posts directly from the query posts property (array)
    if ( $query->posts ) {
        // Setting up the Loop is not strictly necessary here
        // you can use the WP_Post properties directly
        foreach ($query->posts as $post) {
            $output .= sprintf(
                '<a href="%s">%s</a>',
                esc_url( get_permalink( $post->ID ) ),
                esc_html( $post->post_title )
            );
        }
    }

    // Shortcode should return its output
    return $output;
}
add_shortcode('commonposts', 'common_cats');

If you want the category, tag, and custom taxonomy term to be required parameters, you could check that they are not empty in $args, before passing it to WP_Query, and just return empty string, if any of them is.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1741682062a174575.html

最新回复(0)