Home › Forums › Pro Support › Combine Conditions
- This topic has 21 replies, 2 voices, and was last updated 3 years ago by
Tom.
-
AuthorPosts
-
August 21, 2020 at 11:22 am #18454
Philip
ParticipantIs there any way to combine conditions for a list? For example, I’d love to be able to specify to pull posts from a specified category that ALSO have a specified tag. I’m not seeing this, but hoping that there might be a workaround or some way to accomplish it.
Thanks.
August 21, 2020 at 3:27 pm #18468Tom
KeymasterHi there,
This might help: https://wpshowposts.com/support/topic/possible-to-filter-by-a-category-and-a-tag/#post-14933
Let me know 🙂
August 21, 2020 at 7:53 pm #18478Philip
ParticipantThank you, this seems like a perfect solution, but no matter what I try, the settings never seem to have any effect.
I’ve tried setting both fields via the settings and neither in the Dashboard, as well as setting each via the settings and the other in the Dashboard. The one I set in the Dashboard is the only one to ever have any effect – its as if the settings in the shortcode aren’t even there. I’ve also tried using both the category/tag names, as well as their IDs in the settings. For example, I’ve tried:
[wp_show_posts id="93" settings="category__and=styletag__in=homepage"] [wp_show_posts id="93" settings="category__and=8tag__in=68"] [wp_show_posts id="93" settings="category__and=style"] [wp_show_posts id="93" settings="8tag__in=68"] etc.
I noticed in the other post the “category_in” only had one underscore, while according to the WP reference it should have two. So I fixed that – but either way, it does nothing.
Any idea what I’m doing wrong here?
August 21, 2020 at 8:10 pm #18480Philip
ParticipantSorry that code did not come through right. This is what I’ve tried:
[wp_show_posts id="93" settings="category__and=style&tag__in=homepage"] [wp_show_posts id="93" settings="category__and=8&tag__in=68"] [wp_show_posts id="93" settings="category__and=style"] [wp_show_posts id="93" settings="tag__in=68"]
August 22, 2020 at 12:23 pm #18500Tom
KeymasterAh, think I see the issue. The settings don’t have all of the parameters that the queries have.
Something like this will need to wait until WPSP 1.2 so we can filter the query for specific lists, unfortunately.
August 22, 2020 at 4:34 pm #18517Philip
ParticipantIs there anywhere I can see what parameters the settings does have?
Any other way you can think of to get this done? Or any estimate on when 1.2 might be coming?
Thanks
August 23, 2020 at 1:22 pm #18529Tom
KeymasterYou can find them here: https://github.com/tomusborne/wp-show-posts/blob/master/wp-show-posts.php#L97-L145
This is the line that needs updating in the current version: https://github.com/tomusborne/wp-show-posts/blob/master/wp-show-posts.php#L383
It needs to look like this: https://github.com/tomusborne/wp-show-posts/blob/release/1.2/wp-show-posts.php#L391
Then we use this filter:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 123 === $settings['list_id'] ) { $args['category__and'] = 8; $args['tag__in] = 68; } return $args; }, 10, 2 );
You just need to update the
123
with the ID of the list.August 23, 2020 at 3:10 pm #18535Philip
ParticipantThanks Tom, that worked and I could go forward with that.
However, what I was really hoping to do with the wp_show_posts shortcode was use the Lists more like templates. So I could have 2 Lists (with no taxonomy specified in the dashboard) and then use those over and over, wherever I want in various pages, specifying the categories and tags wherever I use them through the settings. So the templates are always the same and the data is different. I don’t think the function you provided above would allow me to do that, since the category and tag are tied to the List ID. Using that, I think I’d need to create dozens of copies of those same Lists and then dozens of copies of that function to match.
Any clever way you know of to accomplish what I want? Maybe something else I can add to that if statement? Basically just some way I can differentiate each instance of the List, so that I can assign a different category/tag combination to each.
Thank you.
August 24, 2020 at 3:21 pm #18558Tom
KeymasterHmm, you could give the list a custom class, then do this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 'my-custom-class' === $settings['wrapper_class'] ) { $args['category__and'] = 8; $args['tag__in] = 68; } return $args; }, 10, 2 );
That way the filter will apply to all lists with that class.
Or are you wanting to change the
8
and68
dynamically?August 24, 2020 at 9:00 pm #18573Philip
ParticipantThat approach could work, but I’d need to be able to set the class via the shortcode in the editor, and whenever I try to do that, I get a fatal error when I try to save, and then have to go remove that code from the database to fix it. This was my shortcode:
[wp_show_posts id="119" settings="wrapper_class=tag1-cat1"]
I kept playing with and found a clunky workaround, using a field that I’m not otherwise using:
[wp_show_posts id="119" settings="read_more_text=tag1-cat1"]
I’d have to then hide that Read More button with CSS, but its doable. I can then do this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 'tag1-cat1' === $settings['read_more_text'] ) { $args['category__and'] = 8; $args['tag__in'] = 68; } elseif ( 'tag1-cat2' === $settings['read_more_text'] ) { $args['category__and'] = 8; $args['tag__in'] = 25; } elseif ( 'tag2-cat1' === $settings['read_more_text'] ) { $args['category__and'] = 10; $args['tag__in'] = 28; } // etc. return $args; }, 10, 2 );
Again, very clunky, but at least this gets me to a place where I don’t need to create dozens of Lists. Any better way you know of to do this now that you can see what I’m trying to accomplish?
August 25, 2020 at 3:07 pm #18588Tom
KeymasterSo why do that instead of reference the
list_id
, exactly? Will there be that many lists?August 25, 2020 at 3:16 pm #18596Philip
ParticipantYes I’ll be using each List format in maybe 20-30 places (multiple times on the homepage plus multiple on every category page).
It’s not a huge amount up front but if I ever want to make changes across all lists, I’ll have to do it to each and every one. For example if I want them all to change to card style or show 4 posts instead of 3, etc.
Seems like it makes more sense just to share 2 listS (one for each format I need).
Or maybe I’m trying to make this plugin do something it’s not meant for and I should use a different one. I’m just using GP so though I’d stay in this ecosystem.
August 26, 2020 at 2:38 pm #18625Tom
KeymasterAnd what will the main difference be between each list?
Let’s say you have 10 lists, how would they be different?
Just trying to get an idea of the overall system you’re using so I can see if there’s a smarter way to go about it.
August 26, 2020 at 3:16 pm #18633Philip
ParticipantThe main difference is just the category and tags to pull from.
So I’d have a bunch of lists that are identical other than the category and tags. Then I’d have a bunch more that are almost the same as the first set but a different format (for example card instead of row) and would pull from other categories and tags.
No two lists would pull from the same category and tag.
So in the situation above we’d ideally have two Lists (one for each format) as opposed to 20 or more.
August 27, 2020 at 2:01 pm #18654Philip
ParticipantAny further thoughts on this one, Tom? If not, I’ll just go forward with my clunky approach.
Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.