Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #999713
    marchesanfilippo
    Participant

    Hi, I used suggestions of your from this thread: https://community.theme.co/forums/topic/recent-posts-shortcode-issue/#post-329816

    I got just the very latest posts, not any matching the tags used.
    Instead, I need only posts matching the same tags used in the post where I put the shortcode as intended – [recent_posts tags=”tagA, tabB, etc”]
    I also tried to modify the arguments as suggested here: http://wordpress.stackexchange.com/questions/219489/random-posts-in-wp-query-when-searching-by-tag

    but finished only breaking the code.
    Could you please help?

    #1000168
    Darshana
    Moderator

    Hi there,

    Could you please post your code implemented. So that we can take a closer look.

    Make sure to wrap your code within the code tags of the forum toolbar.

    Thanks!

    #1001088
    marchesanfilippo
    Participant

    Here it is.

    Please consider also that:

    I made a bit of changes, in order to style the output, not the core code, same as my child theme.
    I use the shortcode not per page, putting it into the classic editor, nor into Cornerstone: it goes inside /my-x-child-theme/framework/views/integrity/wp-single.php
    so it can create a recent posts by tag section on every single post.

    Maybe, this way, global $post is needed in order to make it work as intended, but I need your help as I can’t get exactly how to change your code.

    // *********************** Post correlati by tags
    // https://community.theme.co/forums/topic/filter-recent-posts-by-tag/
    // 
    
    function x_shortcode_recent_posts_with_tag( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => '',
        'tag'         => '',
      ), $atts ) );
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $type          = ( $type        == 'portfolio' ) ? 'x-portfolio' : 'post';
      $count         = ( $count       != ''          ) ? $count : 3;
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $offset        = ( $offset      != ''          ) ? $offset : 0;
      $orientation   = ( $orientation != ''          ) ? ' ' . $orientation : ' horizontal';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
    
         $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} data-fade=\"{$fade}\">";
         // $output = "<div class='x-column x-sm x-1-3 prd_col_news'>";
    
      $args = array(
          'orderby'          => 'date',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}",
      );
    
      if( !empty( $tag ) ) {
          $args['tag'] = $tag;
      }
    
        $q = new WP_Query( $args );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
    
          if ( $no_image == 'true' ) {
            $image_output       = '';
            $image_output_class = 'no-image';
          } else {
            $image_output       = '<div class="">' . get_the_post_thumbnail( get_the_ID(), 'entry-' . x_get_option( 'x_stack', 'integrity' ) . '-cropped', NULL ) . '</div>';
            $image_output_class = 'with-image';
          }
          $output .= '<div class="x-column x-sm x-1-3 prd_col_news prd_rel_tags">';
          $output .= '<a class="" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ) . '">'
                     . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                       . '<div class="entry-wrap prd_clearfix prd_news_wrap">'
                        . '<div class="prd_uprcs prd_post_meta">' . get_the_date() . '</div>'
                           . '<h2 class="prd_ultrabold prd_news_title">' . get_the_title() . '</h2>'
                         . '<div class="prd_cat_img">'.$image_output.'</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
          $output .= '</div>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</article>';             
      $output .= '</div>';
    
      return $output;
    }
    
    add_action('wp_head', 'recent_posts_rebinding');
    
    function recent_posts_rebinding () {
      remove_shortcode( 'recent_posts' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_with_tag' );
    }
    #1001114
    Rad
    Moderator

    Hi there,

    What do you mean by breaking the code? Is there any error displaying? I think it’s best to be tested at your site directly.

    Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password
    – FTP credentials
    – URL that has this recent posts filter by tag

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    Thanks!

    #1001226
    marchesanfilippo
    Participant

    Hi Rad,

    I managed to make all the things work. 🙂

    First of all: I was into a very silly mistake, using a “tags” argument instead of “tag” into the shortcode.
    This made it work like a simple recent post query, ignoring any tag filter.
    As I used the correct argument [recent_posts tag=”taga,tagb,etc”] the section started getting all the post with the same tags as the current one.

    Furthermore, I saw that it was getting all the matching posts including the current one.
    So I added a single item to the query args, in order to make it exclude the current post from the results.

    Hope this help someone else.

      $args = array(
          'orderby'          => 'date',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}",
          'post__not_in' => [get_queried_object_id()],
      );
    #1001273
    Paul R
    Moderator

    Hi,

    Thanks for sharing. Have a great day! 🙂

  • <script> jQuery(function($){ $("#no-reply-999713 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>