Filtering on custom taxonomy with recent posts

I used the code from this thread https://theme.co/apex/forum/t/adding-custom-post-types-to-recent-posts-shortcode/1743 to allow me to display pages with recent posts. However, I need to only display pages of a certain category which I set up as a custom taxonomy for my standard “Page” post type. It is a hierarchical taxonamy like Category for Posts but it is called “Content” and the options are “Ebook”, “Whitepaper”, Video", “Event” etc.

Is this possible?

Hey @scottstone,

Thanks for writing in!
Can you please provide us some more detail about what exactly you want to achieve?
“Ebook”, “Whitepaper”, “Video” these are the categories and you want show these categories as a page. i.e. A page that shows category related to ‘Ebook’. If this is what you want to achieve then you can directly set category items in a menu. Browse to Appearance >> Menu and enable categories from screen options and add categories in menu items.

Let me know if I have misunderstood your issue.

Regards

I have a custom taxonomy for my Pages called Content. In other words, like Posts have Category as a hierarchical taxonomy I have a hierarchical taxonomy for Pages called Content. By modifying my child themes functions.php file I was able to add Pages to the Recent Posts Shortcode that initially only allowed Posts and Portfolio.

My problem is that I cannot further filter the Pages I am calling with Recent Posts Shortcode by the custom taxonomy I have called Content.

I would like to do the following with the shortcode:

[recent_posts type=“page” count=“4” content=“ebook”] analogous to [recent_posts type=“post” count=“4” category=“cool-stuff”]

Hey @scottstone,

So you’re already customizing the shortcode? This is getting into custom development, so while I can’t provide a complete solution I can give you an idea of where to get started.

In the recent posts shortcode, look for where the query is setup. It looks like this:

$q = new WP_Query( array(
    'orderby'             => 'date',
    'post_type'           => "{$type}",
    'posts_per_page'      => "{$count}",
    'offset'              => "{$offset}",
    "{$category_type}"    => "{$category}",
    'ignore_sticky_posts' => $no_sticky
    ) );

You’ll need to modify this query. First, you can remove this line since you won’t be querying based on a category, and instead a custom taxonomy:

      "{$category_type}"    => "{$category}",

Next, take a look at the WordPress codex for WP Query

https://codex.wordpress.org/Class_Reference/WP_Query

Specifically, you’ll want to review the Taxonomy query portion: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

I would actually recommend creating a copy of the recent posts shortcode entirely. This way you can modify it while keeping the original one intact. For example:

function x_shortcode_recent_posts_custom( $atts ) {
  extract( shortcode_atts( array(
    'id'           => '',
    'class'        => '',
    'style'        => '',
    'type'         => 'post',
    'count'        => '',
    'category'     => '',
    'offset'       => '',
    'orientation'  => '',
    // 'show_excerpt' => 'true',
    'no_sticky'    => '',
    'no_image'     => '',
    'fade'         => ''
  ), $atts, 'x_recent_posts_custom' ) );

  $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
  $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';

  $id            = ( $id           != ''     ) ? 'id="' . esc_attr( $id ) . '"' : '';
  $class         = ( $class        != ''     ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
  $style         = ( $style        != ''     ) ? 'style="' . $style . '"' : '';
  $count         = ( $count        != ''     ) ? $count : 3;
  $category      = ( $category     != ''     ) ? $category : '';
  $category_type = ( $type         == 'post' ) ? 'category_name' : 'portfolio-category';
  $offset        = ( $offset       != ''     ) ? $offset : 0;
  $orientation   = ( $orientation  != ''     ) ? ' ' . $orientation : ' horizontal';
  // $show_excerpt  = ( $show_excerpt == 'true' );
  $no_sticky     = ( $no_sticky    == 'true' );
  $no_image      = ( $no_image     == 'true' ) ? $no_image : '';
  $fade          = ( $fade         == 'true' ) ? $fade : 'false';

  $js_params = array(
    'fade' => ( $fade == 'true' )
  );

  $data = cs_generate_data_attributes( 'recent_posts', $js_params );

  $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";

    $q = new WP_Query( array(
      'orderby'             => 'date',
      'post_type'           => "{$type}",
      'posts_per_page'      => "{$count}",
      'offset'              => "{$offset}",
      "{$category_type}"    => "{$category}",
      'ignore_sticky_posts' => $no_sticky
    ) );

    if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();

      if ( $no_image == 'true' ) {
        $image_output       = '';
        $image_output_class = 'no-image';
      } else {
        $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
        $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
        $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
        $image_output_class = 'with-image';
      }

      // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';

      $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( csi18n('shortcodes.recent-posts-permalink'), the_title_attribute( 'echo=0' ) ) ) . '">'
                 . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                   . '<div class="entry-wrap">'
                     . $image_output
                     . '<div class="x-recent-posts-content">'
                       . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                       . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                       // . $excerpt
                     . '</div>'
                   . '</div>'
                 . '</article>'
               . '</a>';

    endwhile; endif; wp_reset_postdata();

  $output .= '</div>';

  return $output;
}

add_shortcode( 'x_recent_posts_custom', 'x_shortcode_recent_posts_custom' );

For those of us not used to coding, where in the above example would I edit in order to specify the custom taxonomy?

Hi there,

Kindly review the previous response step by step. Furthermore, it would be best to seek help from a developer should you not feel comfortable making these changes yourself. We do have an in-house custom development team that offer paid services, who may be able to assist. They can be contacted at [email protected] if this is of interest to you. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.

Thank you for your understanding.