Skip Navigation

[Resuelto] Output posts with conditional taxonomy

This support ticket is created hace 1 año, 5 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 respuestas, has 2 mensajes.

Last updated by Waqar hace 1 año, 4 meses.

Assisted by: Waqar.

Autor
Mensajes
#2504411
Screen Shot 2022-11-26 at 7.33.15 AM.png

Tell us what you are trying to do?
Output posts with conditional taxonomy

Is there any documentation that you are following?
https://toolset.com/forums/topic/use-parents-post-slug-inside-a-conditional-statement/
(Several other documentation pages)

I need to output a list of posts that have a taxonomy slug that includes the current CPT's slug appended with "-agenda"

I've tried several different ways to write the conditional, but I can't seem to get any of them to work.
I even tried registering a custom shortcode found on this page to compare strings:
https://toolset.com/forums/topic/comparing-a-portion-of-a-string-in-conditional-output/#post-1085588
... but I couldn't figure out the syntax to make it work.

Basically, I need to display posts where [wpv-post-taxonomy type="category" format="slug"] equals the current CPT's "[wpv-post-slug]-agenda"

It seems like there should be an easy way to do this.

NOTE: I'm currently working with this in a "classic" view inserted into my content template, but I'd be happy to be able to use the block editor in the content template if it's possible.

#2505245

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

Based on what you've shared, you can use the conditional statement like this:


[wpv-conditional if="( '[wpv-post-taxonomy type="category" format="slug"]' eq '[wpv-post-slug]-agenda' )" ]
This is true!
[/wpv-conditional]

Tip: In case, it still doesn't work as expected, you can enable the debugging in the conditional statement shortcode, to see the exact text/output that is being compared.
( ref: https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[wpv-post-taxonomy type="category" format="slug"]' eq '[wpv-post-slug]-agenda' )" debug="true"]
This is true!
[/wpv-conditional]

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2505515

Greetings Waqar,
Thanks for the reply.
Unfortunately, that conditional didn't quite work.

eq '[wpv-post-slug]-agenda'

Returned the slug of the post in the loop.. I needed it to return the slug of the current page.
I changed it to:

[wpv-conditional if="( '[wpv-post-taxonomy type="category" format="slug"]' eq '[wpv-post-slug id="$current_page"]-agenda' )"]
This is true!
[/wpv-conditional]

... and it seems that this might work.
I need to test a little more before closing the ticket.

#2505945

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that you were able to make it work.

Please take your time and this ticket will stay open for a couple of weeks.

You're welcome to mark it as resolved once confirmed and start a new one for each new question or concern.

#2507501

Waqar,
I'm having problems with this solution...
Because the conditional (filter) is AFTER the query, I can't limit the desired output correctly. It considers ALL of the non-filtered posts in the limit, so it does not display the correct number of posts for the specific matching category of the conditional.

Example: The query is returning ALL posts where the category ends with "-agenda"
AFTER this, The conditional matches and outputs only posts with "current-page-slug-agenda"
This works perfectly, but I don't want to output hundreds of results on this page, so I've added limit 10

The limit(10) includes: "OTHER-page-slug-agenda" in the limit -- but only displays the ones from the conditional.

So, if there are 9 posts in "OTHER-page-slug-agenda" returned first, my output only displays 1 "current-page-slug-agenda" on the page, instead of 10.

Sadly, I don't think there is any way to make this work - unless there is some way to add the limit directly into the conditional?

#2510203

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Here is an alternate approach for this filtering, which takes place at the query level and not once inside the view (through conditional).

1. You'll need a custom shortcode, that can get the current page's slug and return it, removing the '-agenda' part from it.
(so that slug will match the slug of the term from the 'category' taxonomy)


add_shortcode('get_slug_cat_slug_from_page', 'get_slug_cat_slug_from_page_func');
function get_slug_cat_slug_from_page_func() {
	$content = do_shortcode('[wpv-post-slug id="$current_page"]');
	if(!empty($content)) {
		return rtrim($content, '-agenda');
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "get_slug_cat_slug_from_page" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

2. Once this shortcode is ready, you can add a taxonomy query filter in your view so that it only shows the results where the slug of the 'category' taxonomy is the same as the value coming from the shortcode attribute 'wpvcategory'.

3. Next, on the page where you'd like to show this view, you can call it through the view's shortcode like this:


[wpv-view name="name of the view" wpvcategory="[get_slug_cat_slug_from_page]"]

As a result, the custom shortcode will provide the slug to limit the results by, depending on the page the view is called.

I hope this helps and please let me know if you need any further assistance with this.

#2511293

Thanks Waqar,

The current page slug does NOT include "-agenda", so I don't need a shortcode to remove/trim it.

I was appending "-agenda" TO the page slug for the conditional filter, so that it would match the taxonomy, which DOES include "-agenda".

I can return the taxonomy, fine... but my template is used for ALL departments, so I was trying to output the posts in the proper taxonomy conditionally based on current page being viewed (page-slug PLUS -agenda matches wpvcategory).

#2513735

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing this update.

If the use of '-agenda' in the page's slug is not necessary, then yes, you don't need that custom shortcode from my last message.

Did you manage to follow the rest of the steps to filter the view's results, using the shortcode attribute?

#2519543

Waqar,

So the query in my view still needs to return ALL posts, and the wpvcategory="" will then filter those results?
Where do I set the limit?

How is this different from my current code:

[wpv-conditional if="( '[wpv-post-taxonomy type="category" format="slug"]' eq '[wpv-post-slug id="$current_page"]-agenda' )"]
This is true!
[/wpv-conditional]
#2520059

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

> So the query in my view still needs to return ALL posts, and the wpvcategory="" will then filter those results?
- Yes, that is correct, but this filtering will be taking place as a single step and not as two separate steps, at the query level.
( in simple terms, the query will look like this: 'bring all posts where the category is XYX' )

The count of returned results will not be equal to 'ALL' posts, but only equal to the relevant category's posts, which is what you want.

> Where do I set the limit?
- The number of results returned by the view can be further controlled by the view's "Limit and Offset" settings.

> How is this different from my current code
- Your code or the earlier approach would query ALL posts, and then only limit their display through the conditional display, which was leading to a difference in the number of results.
( because the query would return 'X' amount of results, but then because of the conditional display, only 'Y' amount of results would actually be shown on the front-end, as you noted yourself in your reply: https://toolset.com/forums/topic/output-posts-with-conditional-taxonomy/#post-2507501 )

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.