HOW TO SET ORDER BY FOR [RECENT_POSTS] or set all portfolio items to sort by title

There is a super old post here:


I am unsure whether this solution will work, but being 4 years old, I was hesitant to try.

But I wanted to add a shortcode like this to a page to basically add a shortcode to a glossary page I have that would display the portfolio items image and title in alphabetical order. I was also looking at this post:

I did that, added the _portfolio.php to both the theme and child theme just to see if it would work, and I am still not getting my page to sort the item alphabetically on this page: https://carolcustomdesign.com/portfolio-item/

Hi There,

Thanks for writing in! You referenced thread URL above is just 2 days old.

I have connected to your FTP but I’m not sure on which folder that you have setup this site.

Okay I see the issue here. It seems that you’re using and older version of X theme and Cornerstone. You will need to update your X theme and Cornerstone by following this guide (https://theme.co/apex/forum/t/setup-updating-your-themes-and-plugins/62).

Thanks!

I edited the secure note to show which folder you should be looking. But I am confused about not being up to date with theme x? I have theme pro 2.04. And the outdated post I was referring to was the was the first link. I realize the second link is only 2 days old. I posted in that thread and was told to create a new one.
Thank you for looking into this.
Matt

Hello Matt,

I have logged in and check your codes. I replaced the DESC into ASC so that it will display the portfolio titles from A to Z. I’ve also check your custom recent post shortcode. At the moment, it does not work because you do not have any post available.

Please check out my test pages in the secure note.

Thank you for all the work! I am a little confused. I thought that the shortcode you used in an-x-test-2 would output the portfolio items. Basically what the an-x-test page does, but in shortcode form. Basically, how do I insert a shortcode to get the results that are on an-x-test, so I can put it on a custom page with a header?
Thanks again.

Hi @mattkutz,

You can just copy the shortcode the way it’s added to those test pages. Then paste that shortcode to your custom page. Or do you wish to hard code it to your custom page?

Thanks!

From what I can tell, ‘an-x-test’ is just the portfolio template on a page, not an actual shortcode. Or else I would just do that.

Also, I still do not understand why ‘an-x-test-2’ outputs nothing. I was told because I do not have any posts, that is why, but shouldn’t I be able to call posts OR portfolio items with that shortcode depending on type.

[recent_posts_obt count=“4” type=“x-portfolio”]

In the above example shouldn’t that call my custom function, state I want to display 4 and use portfolio items instead of posts?

EDIT: From what I can tell, my custom shortcode recent_posts_obt just does not work. The code that is in my child-theme functions file. Could you verify that it is correct? Although, bottom line my end goal is to create a page that is identical to ‘an-x-test’ but with a header section that would mimic what is now: https://carolcustomdesign.com/gemstones/

Hi there,

Ah yes, that’s portfolio page using a page template. But the shortcode will not work since it’s based on the latest builder where you should allow a post type first. If you’ll going to Admin > Pro > Settings, you’ll notice that the portfolio is not listed as an allowed post type.

$allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );

And even if you made it appear, it’s not going to look the same as the portfolio main page ( an-x-test ).

Your only option is to use Essential Grid (or similar) and create a grid for portfolio then set it’s layout to masonry. Then use its skin builder so you can make it similar to the portfolio page. Please check this https://theme.co/apex/forum/t/extension-essential-grid/68. And there, you can also change the order through additional parameter.

Thanks!

Wonderful. Thanks for all the insight! You guys always rock!

Sidenote: I did enable portfolio post types through the admin > Pro > Settings and my custom shortcode:
[recent_posts_obt count=“4” type=“x-portfolio”]
still does not work. Is there any way you can look into that custom function in my child theme?

If not no worries. I think I will use the grid solution, just am curious what I am doing wrong with that shortcode.

Thanks again.

Hi @mattkutz,

Actually what Rad mentioned is that it is not possible to retrieve that kind of ordered stuff using the Portfolio post type and the cause of the problem.

You anyway need to consider using the Essential Grid plugin to achieve your desired feature.

Thank you.

I was told that the reason my custom recent post shortcode does not work is because I do not have any recent post available. I was expecting that the function I made would allow fro calling portfolio items. Are you saying that the custom recent post function I made only works with only posts, not portfolio items?

Hello There,

I have investigated your issue and it turns out that there is a fatal error in your code. Please have it updated and use this instead:

// Recent Posts
// =============================================================================

function x_shortcode_recent_posts_new( $atts ) {
  extract( shortcode_atts( array(
    'id'          => '',
    'class'       => '',
    'style'       => '',
    'type'        => 'post',
    'count'       => '',
    'category'    => '',
    'offset'      => '',
    'orientation' => '',
    'no_image'    => '',
    'fade'        => ''
  ), $atts, 'recent_posts_obt' ) );

  $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';
  $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'          => 'title',
      'order'            => 'DESC',
      'post_type'        => "{$type}",
      'posts_per_page'   => "{$count}",
      'offset'           => "{$offset}",
      "{$category_type}" => "{$category}"
    ) );

    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';
      }

      $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( __( 'Permalink to: "%s"', 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>'
                     . '</div>'
                   . '</div>'
                 . '</article>'
               . '</a>';

    endwhile; endif; wp_reset_postdata();

  $output .= '</div>';

  return $output;
}

add_shortcode( 'recent_posts_obt', 'x_shortcode_recent_posts_new' );

We would loved to know if this has work for you. Thank you.

Works wonderful, especially once I change the type to portfolio. Exactly what I wanted. Cheers

1 Like