[Support request] Featured category post

Home Forums Support [Support request] Featured category post

Home Forums Support Featured category post

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #1924182
    Vicky

    Hi there,

    I’m using a header element on my category archive pages to show a featured post. I’ve done this using a tag eg. ‘featured-recipes’ and the WP Showposts plugin. That same featured post has then been excluded from the rest of the category archive page.

    My client would like to be able to add a new category themselves eg ‘food’, create a tag eg. ‘featured-food’, and have the featured post for that category automatically show up in the header element, is it possible?

    Thanks in advance!

    #1924519
    David
    Staff
    Customer Support

    Hi there,

    if i understand correctly you would need to:

    1. Dynamically display a post in the Category archive header that is:
    a) in the Current Category
    b) has a Tag of featured-something

    2. Remove the featured-something post from the main loop in the current archive.

    Is that correct ?

    #1924531
    Vicky

    Hi David,

    Yes, that’s exactly what I’m trying to do.

    Wasn’t sure how well I’d explained it!

    Thanks

    #1924797
    David
    Staff
    Customer Support

    You explained it very well … sometime my brain doesn’t absorb stuff until i hear it twice lol

    To do that would require the WP Show Posts Beta version as it has the $settings options that will allow us to filter by categories and tags:

    https://github.com/tomusborne/wp-show-posts/tree/release/1.2

    Then you could:

    1. Create Post Tag that will be used for all Featured Posts eg. featured post. Make a note of its ID
    2. Create a single WP Show Posts list, that you add to your element, and set to Display on ALL Post Category Archives. Again make a note of its ID.
    3. Add this PHP Snippet to filter your WPSP list:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
    	$category = get_queried_object();
    	$cat_id = $category->term_id;	
        if ( 103 === $settings['list_id'] ) {
            $args['category__and'] = $cat_id;
            $args['tag__in'] = 12;
        }
    
        return $args;
    }, 10, 2 );

    103 needs to be changed to the WPSP list ID.

    And 12 is the ID of the tag .

    On that note you would create a common tag used for all featured posts. eg. ‘featured-post’

    #1925454
    Vicky

    Hi David,

    Thanks for your reply, but I’m not sure if that is going to work for me.

    I tried before having one tag ‘featured-post’ and the problem was on some category pages there was sometimes more than one post with the featured tag – this is because each post has multiple categories assigned to it.

    And without knowing exactly what post is going to be the featured post you can’t then exclude that exact post from the rest of the page – that is why I was thinking of having a unique tag for each category page.

    Again, hope that makes sense!

    Not sure if your solution accounts for this, if it does I’ll give it a go.

    Thanks in advance,
    Vicky

    #1925553
    David
    Staff
    Customer Support

    Ooh thats a tricky one… not sure if this will work but you could try this:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        $category = get_queried_object();
        $cat_id = $category->term_id;	
        $cat_slug = $term->slug;
        $featured_tag = 'featured-' . $cat_slug;
        $tag_id = get_term_by('slug', $featured_tag ,'post_tag');
        if ( 103 === $settings['list_id'] ) {
            $args['category__and'] = $cat_id;
            $args['tag__in'] = $tag_id;
        }
        return $args;
    }, 10, 2 );

    The way it ‘may’ work is by having a tag slug of: featured-categorySlug

    So if you had a category term of: food
    You would add a Tag term of featured food

    The code will grab the category slug ie. food and append ut to a string of featured- resulting in featured-food slug that we then grab the ID for that tag….

    Might need some tweaking.

    #1928589
    Vicky

    Hi there,

    Thanks for your reply, this looked hopeful, but it’s not quite working.

    This is what I’ve done:

    • I’ve added wp-showposts v.1.2 and set up wp-showposts to show all posts (no categories or tags selected)

    • Then got the ID 9839, and replaced 103 in the php you provided.

    • Then added the shortcode for wp-showposts to the element, which is set to display on all category archives.

    • Then added a tag ‘featured wine’ and got slug featured-wine and added this tag to a post.

    I’m just getting the same post which is the latest post showing up on all the category archive pages.

    Thanks in advance,
    Vicky

    #1929641
    Elvin
    Staff
    Customer Support

    Hi Vicky,

    Can you try this?

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        $category = get_queried_object();
        $cat_id = $category->term_id;	
        $cat_slug = $category->slug;
        $featured_tag = 'featured-' . $cat_slug;
        $tag_id = get_term_by('slug', $featured_tag ,'post_tag');
        if ( 9839 === $settings['list_id'] ) {
            $args['category__and'] = $cat_id;
            $args['tag__in'] = $tag_id;
        }
        return $args;
    }, 10, 2 );
    #1929797
    Vicky

    Hi Elvin,

    Thanks for your suggestion, but it doesn’t seem to have made any difference – I’m still just getting the same latest post showing up on all category archive pages.

    #1929852
    Elvin
    Staff
    Customer Support

    Let’s go by slugs.

    Can you try this?

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
    
        $category = get_queried_object();
        $cat_id = $category->term_id;	
        $cat_slug = $category->slug;
        $featured_tag = 'featured-' . $cat_slug;
        if(9839 === (int)$settings['list_id']){
    	$args['tax_query'] = array(
    			'relation' => 'OR',
    			array(
    				'taxonomy' => 'category',
    				'field'    => 'slug',
    				'terms'    => $cat_slug,
    			),
    			array(
    				'taxonomy' => 'post_tag',
    				'field'    => 'slug',
    				'terms'    => $featured_tag,
    			),
        	);
      }
      return $args;
    }, 15, 2 );

    This code assumes you actually have existing featured_xxxxx tags depending on the category slug. It doesn’t create the tags for you. WPSP can’t do that. This is only for query. 😀

    #1929916
    Vicky

    Hi Elvin,

    Thanks for looking into this, I’ve added your code and it worked for a couple of posts that already had tags associated with them. But when I added a new tag or changed the tag for a post it didn’t work. Is that what should be happening?

    What I actually want to do is to be able to add a tag eg. ‘featured recipe’ and have that automatically show up in an element at the top of a category archive – this is so that the client doesn’t have to set up the element for each category they create in the future.

    Maybe it’s not possible, or maybe there’s an alternative way of doing this, open to suggestions!

    Link below to an example of what I’m trying to set up.

    Thanks in advance
    Vicky

    #1930854
    Elvin
    Staff
    Customer Support

    It will show posts as long as there are posts with either the specified category or specified tag.

    Example:

    If you’re in category “cats”, the list would show posts from either category “cats” or post tag “featured_cats”.

    If no posts under featured_cats exist, it will only show the ones w/ cats category.

    This code doesn’t make tags for you. It only displays posts from a specified tag or category.

    If what you actually want is automatic tag creation, you need a separate plugin for that.

    See this article for example – https://wpmissing.com/plugins/generate-tags-post-title-category/

    #1931156
    Vicky

    Hi, thanks for the explanation, but I don’t actually want to automatically create tags. All the tags on my site will be created manually in the normal way – hope that clarifies things.

    I’ve attached a couple of screenshots to show the problem. One shows the posts in the admin dashboard – you can see categories and tags for the posts. The other screenshot shows the ‘popular’ category page. You can see the post with ‘Featured Popular’ tag (slug: ‘featured-popular’) is not showing up at the top.

    Do you know why that might be?

    Thanks in advance,
    Vicky

    #1932061
    Elvin
    Staff
    Customer Support

    I’m not sure I see what you mean.

    The screenshot seems to have 2 queries. The one on the white background and the one on the green background.

    And the setup looks like:

    The one on the white background looks like its post_per_page = 1. Which may or may not show a post with a tag featured-popular depending on the orderby value because it will only show the first post of the query regardless if it has both featured-popular tag or popular category.

    Now if you want the post list on the white background to display a post that must have category of popular AND featured-post tag, you’ll have to change its query to 'relation' => 'AND'.

    #1932273
    Vicky

    Hi Elvin,

    Awesome, this has worked!

    The white area at the top is now showing the post with ‘featured-popular’ tag on the ‘popular’ category page – that’s exactly what I am after.

    However, as you pointed out there are 2 queries on the page. On the green background where all the posts for the category are listed, the problem is that the featured post is showing there too. Is there a way to automatically exclude the featured post from the green background area?

    Thanks in advance,
    Vicky

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.