- This topic has 17 replies, 3 voices, and was last updated 2 years, 6 months ago by Elvin.
-
AuthorPosts
-
September 9, 2021 at 6:59 am #1924182Vicky
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!
September 9, 2021 at 8:54 am #1924519DavidStaffCustomer SupportHi 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-something2. Remove the featured-something post from the main loop in the current archive.
Is that correct ?
September 9, 2021 at 9:03 am #1924531VickyHi David,
Yes, that’s exactly what I’m trying to do.
Wasn’t sure how well I’d explained it!
Thanks
September 9, 2021 at 1:33 pm #1924797DavidStaffCustomer SupportYou 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’
September 10, 2021 at 6:06 am #1925454VickyHi 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,
VickySeptember 10, 2021 at 7:26 am #1925553DavidStaffCustomer SupportOoh 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 offeatured food
The code will grab the category slug ie.
food
and append ut to a string offeatured-
resulting infeatured-food
slug that we then grab the ID for that tag….Might need some tweaking.
September 13, 2021 at 2:52 am #1928589VickyHi 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,
VickySeptember 13, 2021 at 9:20 pm #1929641ElvinStaffCustomer SupportHi 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 );
September 14, 2021 at 1:24 am #1929797VickyHi 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.
September 14, 2021 at 2:31 am #1929852ElvinStaffCustomer SupportLet’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. 😀September 14, 2021 at 3:40 am #1929916VickyHi 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
VickySeptember 14, 2021 at 8:23 pm #1930854ElvinStaffCustomer SupportIt 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/
September 15, 2021 at 4:05 am #1931156VickyHi, 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,
VickySeptember 15, 2021 at 8:01 pm #1932061ElvinStaffCustomer SupportI’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'
.September 16, 2021 at 1:19 am #1932273VickyHi 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 -
AuthorPosts
- You must be logged in to reply to this topic.