Skip Navigation

[Resuelto] correct filter for related posts

This support ticket is created hace 7 años, 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Etiquetado: 

This topic contains 8 respuestas, has 2 mensajes.

Last updated by christerA hace 7 años, 5 meses.

Assisted by: Nigel.

Autor
Mensajes
#459044

Hi.
I have a custom post type with a category structure, like:
Books
- Thrillers
- Sci-Fi
- Romance
All my books are set to both category "Books" and the correct subcategory.
When i view any of the books in the subcategory i want to display all related books in that subcategory in a list beside the book info.
I have a View with Taxonomy filter of that category.
But the problem is:
My Related Posts show things from "Books", which is not what i want.
If i view a "Sci-Fi", i only want to show other books in that subcategory.
What are the correct settings for this?.

#459067

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+01:00)

Hi.

There aren't any correct settings for this because the taxonomy filter used to qualify which posts are returned is comparing the categories of each post against the post where the view is added, so a SciFi book will match all books because both have the category book, i.e. it is expected behaviour.

To modify this you will need to write a custom code snippet that uses wpv_filter_query (https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/) to change the query arguments.

Here is the query debug info from a quick test I added (using the standard post type) with categories as you describe in your question.

Array
(
    [post_type] => Array
        (
            [0] => post
        )

    [paged] => 1
    [suppress_filters] => 
    [ignore_sticky_posts] => 1
    [posts_per_page] => -1
    [post__not_in] => Array
        (
            [0] => 171
        )

    [wpv_original_limit] => -1
    [wpv_original_offset] => 0
    [wpv_original_posts_per_page] => -1
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => category
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 49
                            [1] => 50
                        )

                    [operator] => IN
                    [include_children] => 1
                )

            [relation] => AND
        )

    [post_status] => Array
        (
            [0] => publish
            [1] => private
        )

    [orderby] => date
    [order] => DESC
)

Note the key part here is the "[operator] => IN" which is an OR operator (if the posts have either the Book or SciFi categories, for example. You need to change that to AND, so that posts will only match if they have both the Book and SciFi categories.

If you need further help with that, let me know.

#459110

Hi.
Thank you for your reply, i will try that.
I was really hoping for an easier solution (with less code...).
So:
What's your opinion about all those "wordpress related posts plugins".
Have you any experience with any of them? And would one of these possibly solve this specific problem?
Any you would recommend? And any that works well with Toolset?

#459115

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+01:00)

A related posts plugin seems like overkill in this case.

If you want to avoid any coding, what I would do is, instead of having one hierarchical taxonomy, to split it into two taxonomies.

So if you top level taxonomy has terms books and... what else... magazines, DVDs etc., and your lower tier terms are for SciFi, Romance etc. then I would make a taxonomy called "media" which has terms "books, magazines, DVDs, whatever", and a separate taxonomy, let's say, "genre" with "SciFi, Romance, Crime" etc.

Then your view for related books in the same genre would have two taxonomy filters, one for media, one for genre.

#459144

Hi.
You mean "Non-Hierarchical Taxonomy" (tags)?
I already have hundreds of Books in the site, so i want to change as little as possible. I also have about 100 subcategories of these Books.

You wrote:
"let's say, "genre" with "SciFi, Romance, Crime" etc."
But that won't work, will it?
I mean, when i'm viewing a "Sci_Fi" book, won't the "Related Posts" show things from "Romance" and "Crime" too?
I want only related "Sci-Fi" books to show up.

It's a lot of work, but won't i have to create 100 tags?
I really hope there is an easier solution.

#459149

Hi again.
Can't we do this with Views "Conditional statements", somehow?
All i really want is:

In my "View Show Related Books", i want a conditional statement that says:
- Ignore all "Books". That is, the parent category
- Just show the Books in the current subcategory, like "Sci-Fi".

#459375

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+01:00)

The term 'hierarchical' to distinguish between a category-type taxonomy and a tag-type taxonomy is not very helpful here, I was suggesting continuing using categories but making the hierarchy just one level deep.

The problem generally, and specifically for your last suggestion, is that all books have the books category in common, and as long as that's the case, all books will show up in a query that looks for posts with matching categories.

If you don't actually need the Books category (because there is no magazines, no DVD, no whatever other top level category) then just delete it. Then SciFi books will have the same category as other SciFi books but not as Crime books. I don't know whether that is an option, I don't know if you have sections which depend on the presence of the Books category.

I'll see if I can set up a test site with a code snippet to get this working as I described initially, and I'll let you know what I come up with.

#459397

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+01:00)

Here is a small code snippet that should do the job.

You will need to edit the view id accordingly, and, note that I wrote this with a plain vanilla view in mind that just uses a taxonomy filter like so: "Select posts with taxonomy: Categories the same as the current post in the loop".

If you are doing something more complex then this might not work.

function modify_tax_query( $view_args, $view_settings, $view_id ){

	if ( $view_id == 177 ) { // edit as required

			$view_args['tax_query'][0]['include_children'] = 0;
			$view_args['tax_query'][0]['operator'] = 'AND';
	}

	return $view_args;
}
add_filter( 'wpv_filter_query', 'modify_tax_query', 101, 3 );

Add that to your functions.php file and let me know how you get on.

#459579

Hi.
I will test this. I don't have time to do it right now, so we can close this ticket for now.
Thank you for all help.

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