$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Get Posts by Category, Tag , and CPT Taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far