Search Functionality and Hidden Categories

Hello Dear Themeco

I could use help with some simple (I hope) matters.

  1. First thing is the behaviour of search functionality. I’d like it to show only results from posts by default, not from pages too as it does now.

  2. Second thing is I’d like to show my custom post type posts in the same blog main page as regular ones, mixed in with them. I’m using the:

add_action( ‘pre_get_posts’, ‘my_get_posts’ );
function my_get_posts( $query ) {
if ( ( is_home() && $query->is_main_query() ) || is_feed() )
$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘pod_quotes’) );
return $query;
}

…but for some reason it doesn’t work. It does work for “page” so the code is working correctly it’s just X Theme doesn’t want to show custom post type for some reason.

Custom post type is made using pods and it shows just fine using permalink.

  1. Third thing is I need certain functionality (whether natively if X Theme supports it or as plugin recommendation). That functionality is social Like/Follow buttons that I could put in my footer as widget. What I want those buttons to do is NOT to link user to respective fanpage but to hook to respective social network’s API and directly “Like” or “Follow” the respective fanpage without visiting it. I know some pages have that for Facebook so it’s possible. Answer to question “how” eludes me though. I’d also like those buttons to feature social followers count pulled from respective social networks, but that I can live without if needed.

ADDITIONAL INFO:

  • I’m using Ethos Stack.
  • I’m using X Theme not Pro.
  • I’m working on localhost so I can’t give You access to site.

Hello There,

Thanks for writing in!

1.) To exclude pages and other custom post types for the search results, please check out this link:

2.) And to include the custom post type in your blog index, please check this out:


Your code seems in the right path. I am not sure of adding the page is a good idea. The pod_quotes might not be the correct custom post type slug which could be why it does not work. Please review and make sure that you have the correct custom post type slug. It could only be pod_quote.

3.) You might need to install a 3rd party plugin that has a widget feature where in you can add the widget in the footer widget areas for your site. You might need to check out any of this plugins:
https://wordpress.org/plugins/search/facebook+like/

Hope this helps.

Hello RueNel, I’m glad I’m talking with You again :blush:

  1. Works like a charm, I have no idea how I failed to google this one (I promise I tried). Thank You very much.

  2. Slug is correct, I named it pod_quotes myself for testing purposes. Easier to remember this way. I also tested code from both sites You posted and variations on many other sites and for some reason I can’t make custom posts to appear on blog mixed with regular posts.

As for adding pages I did it only for testing purposes, to check if the snippet works at all. It does. It’s just that custom post type for some reason doesn’t cooperate with X Theme or I did something else wrong. Maybe there is some Ethos or X Theme Global template file that restricts blog to regular posts and takes precedense over functions.php? Is it a possibility? If so how can I add custom post type by other means than functions.php filter?

  1. Yeah I know. I just hoped You can recommend some plugin directly, either from Your experience or from feedback from Your other customers. Thing is, all plugins I find show share count and share buttons, while what I need is total follower count and follow/like button.

  2. My title mentioned one more thing, I was too sleepy the other night to mention it in the end. I have a problem regarding categories. I have several of them that are strictly internal and I don’t want them to be shown in post meta on front-end. One solution I found is to display:none on those classes in post meta but it leaves empty space in between two commas and it looks bad. I’d rather filter those out somehow. Just to be clear I only want to filter out category names from post meta, not posts themselves.

Thanks in advance again, RueNel :blush:

Hello There,

Thanks for updating in!

2.) Are you using something like this?

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', 'pod_quotes' );
  return $query;
}

Could you please provide us a url of one of your pod quotes so that we can inspect it?

3.) Some of the users does not use any plugin. They just simply use a text widget or html widget and get an embedded code from Facebook Developer site.
https://developers.facebook.com/docs/plugins/

4.) If you want to filter out the categories in the post meta, you may need to re code the post meta again. Please add the following code in your child theme’s functions.php file

// Ethos Entry Meta
// =============================================================================

if ( ! function_exists( 'x_ethos_entry_meta' ) ) :
  function x_ethos_entry_meta() {

    //
    // Author.
    //

    $author = sprintf( ' %1$s %2$s</span>',
      __( 'by', '__x__' ),
      get_the_author()
    );


    //
    // Date.
    //

    $date = sprintf( '<span><time class="entry-date" datetime="%1$s">%2$s</time></span>',
      esc_attr( get_the_date( 'c' ) ),
      esc_html( get_the_date() )
    );


    //
    // Categories.
    //

    if ( get_post_type() == 'x-portfolio' ) {
      if ( has_term( '', 'portfolio-category', NULL ) ) {
        $categories        = get_the_terms( get_the_ID(), 'portfolio-category' );
        $separator         = ', ';
        $categories_output = '';
        foreach ( $categories as $category ) {
          $categories_output .= '<a href="'
                              . get_term_link( $category->slug, 'portfolio-category' )
                              . '" title="'
                              . esc_attr( sprintf( __( "View all posts in: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                              . '"> '
                              . $category->name
                              . '</a>'
                              . $separator;
        }

        $categories_list = sprintf( '<span>%1$s %2$s',
          __( 'In', '__x__' ),
          trim( $categories_output, $separator )
        );
      } else {
        $categories_list = '';
      }
    } else {
      $categories        = get_the_category();
      $separator         = ', ';
      $categories_output = '';
      foreach ( $categories as $category ) {

        $cat_filter   = array('cat1', 'cat2', 'cat3');

        if ( ! in_array($category, $cat_filter) ) {

          $categories_output .= '<a href="'
                            . get_category_link( $category->term_id )
                            . '" title="'
                            . esc_attr( sprintf( __( "View all posts in: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                            . '"> '
                            . $category->name
                            . '</a>'
                            . $separator;

        }

      }

      $categories_list = sprintf( '<span>%1$s %2$s',
        __( 'In', '__x__' ),
        trim( $categories_output, $separator )
      );
    }


    //
    // Comments link.
    //

    if ( comments_open() ) {

      $title  = apply_filters( 'x_entry_meta_comments_title', get_the_title() );
      $link   = apply_filters( 'x_entry_meta_comments_link', get_comments_link() );
      $number = apply_filters( 'x_entry_meta_comments_number', get_comments_number() );

      $text = ( 0 == $number ) ? __( 'Leave a Comment', '__x__' ) : sprintf( _n( '%s Comment', '%s Comments', $number, '__x__' ), $number );

      $comments = sprintf( '<span><a href="%1$s" title="%2$s" class="meta-comments">%3$s</a></span>',
        esc_url( $link ),
        esc_attr( sprintf( __( 'Leave a comment on: &ldquo;%s&rdquo;', '__x__' ), $title ) ),
        $text
      );

    } else {

      $comments = '';

    }


    //
    // Output.
    //

    if ( x_does_not_need_entry_meta() ) {
      return;
    } else {
      printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>',
        $categories_list,
        $author,
        $date,
        $comments
      );
    }

  }
endif;

And do not forget to change the category names that you want to filter out. Substitute the $cat_filter = array('cat1', 'cat2', 'cat3'); with the actual category names in the code above.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.