How to display posts that are common in Category A and Tag A, and I want to display posts by shortcode so that the Shortcode can be used on multiple locations with different category Ids and Tags.

I found the below code by google but don't know how to implement it in functions.php and not sure how to use it in the shortcode. I want to display Post Title, And Some Custom Field Value [For Example - Value A, Value B] in a table structure.

        $args = array(
        'category__and' => 'category', //must use category id for this field
        'tag__in' => 'post_tag', //must use tag id for this field
        'posts_per_page' => -1); //get all posts
        $posts = get_posts($args);
        foreach ($posts as $post) :
       //do stuff 
  • Are you asking how to create a shortcode? Or how to retrieve post meta inside a loop? We have a 1 question per question policy, so be specific. You can always ask follow up questions on new questions
    – Tom J Nowell
    Jan 4, 2021 at 16:56
  • I want to display common posts from specific Tag A and Category A, But this should happen with help of shortcode. Apologize for the lack of my explanation skill.
    – Puneet
    Jan 4, 2021 at 17:21
  • I understood that, but you appear to be trying to do several things and combine them, but don't know how to do each part. You're asking 3 separate questions 1: "How do i create a shortcode?" 2: "How do I list posts in a category?" 3: "How do I display a custom field?", there are 3 separate questions, not 1.
    – Tom J Nowell
    Jan 4, 2021 at 19:31

1 Answer 1

function common_cats($att){
 $args = array(
        'category__and' => $att['category'], //must use category id
        'tag__in' => $att['tag'], //must use tag id for this field
        'posts_per_page' => $att['posts_per_page']); //get all posts
        $posts = get_posts($args);
        $output = "<ul>";
        foreach ($posts as $post) :
           $output .= "<li>".get_the_title(). "</li>";
        $output .= "</ul>"; 
return $output;
add_shortcode('commoncats', 'common_cats');

use [commoncats] where you like to show the output of the above code. above code will return a list of titles.

  • Hey @Muneeb, I have tried out [commoncats tag="32" category="5" posts_per_page="50"] But nothing display. I also want to echo the custom field values.
    – Puneet
    Jan 4, 2021 at 16:24
  • I have updated the check if it works for you, for custom fields; you want to display a specific field or all fields?
    – MMK
    Jan 4, 2021 at 16:45
  • category__and requires an array of IDs, not just a string/number, it's the same with tag__in . This would be better with a WP_Query loop instead, and proper indenting
    – Tom J Nowell
    Jan 4, 2021 at 16:54
  • @PuneetVerma note that you will need basic programming skills to understand answers here, this isn't a place you can request a copy paste solution
    – Tom J Nowell
    Jan 4, 2021 at 16:54
  • @Muneeb, Specific custom fields not all, This works but I have implemented on page (A) but it's print that Page's (A) Title for 50 Times.
    – Puneet
    Jan 4, 2021 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.