Multiple Category order
-
Hi,
I’m trying to setup your plugin on my site but there’s an issue with multiple categories.
e.g https://beautyblackbook.com/mother-dirt-topical-probiotics-in-skincare/
I’ve set the settings to show related posts from the current post category.
There’re about 10 categories assigned, one is set to default, but on the page you see posts from different categories.Most likely on that page it displays posts from https://beautyblackbook.com/category/search-by-issue/acne/
but I don’t know why.Do you know how can I change it to use the default category instead?
Regards,
Alex
-
Hi,
if you activate the option “Get posts from current category” the plugin will do this (as explained in the widget itself):When activated, this function will get posts from the first category of the post, ignoring other parameters like tags, date, post formats, etc. If the post has multiple categories, the plugin will use the first category in the array of categories (the category with the lowest key in the array).
In fact, if we extracts the categories of a given post, we’ll see something like this:
Array ( [0] => 2 [1] => 9 [2] => 10 [3] => 11 [4] => 29 )
where
2
,9
,10
,11
, and29
are the IDs of the categories assigned to the current post. The plugin will get the posts with the category2
, that is the category with the lowest ID (0
).Thanks for the explanation, now I understand the issue.
PS. I’m not a developer, it took me a long time to understand this stuff)Checked the array and it looks the way you described.
array(30) { [0]=> int(395) [1]=> int(615) [2]=> int(434) [3]=> int(394) }But I do use Yoast Seo Plugin and it has a feature – Primary Category. I’ve found where the ID is stored, but don’t know how to proceed further.
Is there a way I can change your plugin behaviour so, that in case of multiple categories it will get the category ID from the custom meta_key in wp_postmeta table?
e.g
6506, ‘_yoast_wpseo_primary_category’, ‘705’),Where the 1st value is post ID and the last is the category id I need the posts to be displayed from.
Hello,
there is already a section to get posts from taxonomy using custom field, but currently it get posts from a custom field using a slug, not a taxonomy ID. The option is under Getting posts > Change the query when on single posts > Get posts from taxonomy using custom field. I wrote a (poor) explanation here.I’ll take a look if it’s possible to use IDs too.
Let me look at this and I’ll let you know in a few days.
Oh, that may help.
I’ve tried it on 7 posts and it worked perfect, but the problem is that I have about 200 posts and updating all of them with new custom field will take quite a lot of time.
ID option would be much better.
Thank you very much for looking into it!
Sorry to bother you, but have you got a chance to check if the ID option is possible?
Thank you in advance
Hello @morfe1
sorry for the delay, but I’m currently very busy. I hope to work on it this weekend.Hello @morfe1,
I just updated the plugin.Before pushing it to the WordPress repository, I’d like if you could make a test and tell me back.
The updated plugin’s files are on GitHub: https://github.com/aldolat/posts-in-sidebar. You should update manually the plugin’s files.
Let me know, please.
Hi there,
Thank you very much for looking into it, I’ve tested the updated version.
It’s a way better now, however few posts from another categories still appear in the results.
I put ‘_yoast_wpseo_primary_category’ as a custom taxonomy key and custom taxonomy is the only checked option.
Couldn’t paste everything here. Here is the rest with the results of testing:
https://pastebin.com/6hpgFVFKBest Regards,
AlexHere are the results for 3 posts:
1) new 4/6 correct results vs old 0/4https://hidemyage.staging.wpengine.com/hydrafacial-md/
vs
https://beautyblackbook.com/hydrafacial-md/right category id – 385 (daily skincare)
postID 7141 with primary_categoryID 1495 ( Beauty Products & Devices)
https://hidemyage.staging.wpengine.com/le-mieux-skin-perfecter/postID 3490 with primary_categoryID 1497 (ANTIAGING SKINCARE EDUCATION)
https://hidemyage.staging.wpengine.com/snail-facial/2) new 3/6 vs old 0/4 results
https://hidemyage.staging.wpengine.com/thermismooth-face/
Vs
https://beautyblackbook.com/thermismooth-face/right category id 710 (LASER SKIN REJUVENATION)
post 7071 primary_categoryID 706 (body)
https://hidemyage.staging.wpengine.com/ultrashape-power-fat-burning/post 6442 primary_categoryID (none)
https://hidemyage.staging.wpengine.com/what-is-dual-mode-microneedling/post 5958 primary_categoryID 1495 (Beauty Products & Devices)
3) new 5/6 vs 1/1 (don’t know why it doesn’t show any other)
https://hidemyage.staging.wpengine.com/brilliant-distinctions-program/
vs
https://beautyblackbook.com/brilliant-distinctions-program/right category id – 389 (Injectables)
post 7337 – primary_categoryID 2163 (BOTOX ALTERNATIVES)
https://hidemyage.staging.wpengine.com/rt002-by-revance/Hope it helps.
As you see it’s getting 60-70% correct posts. I can live with that (even 60% is a way better than 0%, haha) but if you want to find out the culprit I will gladly assist with testing.
Hi there,
Thank you very much for looking into it, I’ve tested the updated version.
It’s a way better now, however few posts from another categories still appear in the results.
I put ‘_yoast_wpseo_primary_category’ as a custom taxonomy key and custom taxonomy is the only checked option.
Here are the results for 3 posts:
1) new 4/6 correct results vs old 0/4https://hidemyage.staging.wpengine.com/hydrafacial-md/
vs
https://beautyblackbook.com/hydrafacial-md/right category id – 385 (daily skincare)
postID 7141 with primary_categoryID 1495 ( Beauty Products & Devices)
https://hidemyage.staging.wpengine.com/le-mieux-skin-perfecter/postID 3490 with primary_categoryID 1497 (ANTIAGING SKINCARE EDUCATION)
https://hidemyage.staging.wpengine.com/snail-facial/2) new 3/6 vs old 0/4 results
https://hidemyage.staging.wpengine.com/thermismooth-face/
Vs
https://beautyblackbook.com/thermismooth-face/right category id 710 (LASER SKIN REJUVENATION)
post 7071 primary_categoryID 706 (body)
https://hidemyage.staging.wpengine.com/ultrashape-power-fat-burning/post 6442 primary_categoryID (none)
https://hidemyage.staging.wpengine.com/what-is-dual-mode-microneedling/post 5958 primary_categoryID 1495 (Beauty Products & Devices)
3) new 5/6 vs 1/1 (don’t know why it doesn’t show any other)
https://hidemyage.staging.wpengine.com/brilliant-distinctions-program/
vs
https://beautyblackbook.com/brilliant-distinctions-program/right category id – 389 (Injectables)
post 7337 – primary_categoryID 2163 (BOTOX ALTERNATIVES)
https://hidemyage.staging.wpengine.com/rt002-by-revance/Hope it helps. I can live with that (even 60% is a way better than 0%, haha) but if you want to find out the culprit I will gladly assist with testing.
Best regards,
AlexHi @aldolat,
Thanks for the update, it’s a way better now.
However for some reason few posts from another categories still appear in the results.
So I’ve chosen ‘Get posts from taxonomy using custom field’, put ‘_yoast_wpseo_primary_category’ into the key field and ‘Type of the taxonomy’ remained ‘Category’
Results of my testing (I’ve compared 3 posts):
1) new 4/6 correct vs old 0/4 correct results
https://hidemyage.staging.wpengine.com/hydrafacial-md/
vs
https://beautyblackbook.com/hydrafacial-md/right category id – 385 (daily skincare)
post 7141 def_cat 1495 ( Beauty Products & Devices)
https://hidemyage.staging.wpengine.com/le-mieux-skin-perfecter/post 3490 def_cat 1497 (ANTIAGING SKINCARE EDUCATION)
https://hidemyage.staging.wpengine.com/snail-facial/2) new 3/6 vs old 0/4 results
https://hidemyage.staging.wpengine.com/thermismooth-face/
Vs
https://beautyblackbook.com/thermismooth-face/right category id 710 (LASER SKIN REJUVENATION)
post 7071 def_cat 706 (body)
https://hidemyage.staging.wpengine.com/ultrashape-power-fat-burning/post 6442 def_cat (none is set, don’t know why it pulled this one)
https://hidemyage.staging.wpengine.com/what-is-dual-mode-microneedling/post 5958 def_cat 1495 (Beauty Products & Devices)
3) new 5/6 vs 1/1 (don’t know why it doesn’t show any other)
https://hidemyage.staging.wpengine.com/brilliant-distinctions-program/
vs
https://beautyblackbook.com/brilliant-distinctions-program/right category id – 389 (Injectables)
post 7337 – def_cat 2163 (BOTOX ALTERNATIVES)
https://hidemyage.staging.wpengine.com/rt002-by-revance/So as you see 60-70% results are correct, while it was close to 0% before.
I can live with that, but if you want to find a culprit I will gladly help with testing)Best regards,
AlexHi @morfe1
in my tests, the code is working 100% correctly. When viewing a single post page, the code retrieves the posts that are under the category (or tag, if I have defined a tag) specified in the custom field value. The value can be either a string or a numeric, as you requested.Please, make sure you that:
– you have a custom field key shared across the posts you want to retrieve, i.e. the posts have the same custom field key;
– the custom field value is the term slug or the term ID;
– the post with this custom field key must be under this term;
– go to the widget and under “Get posts from taxonomy using custom field” activate “When on single posts, get posts from this custom field”, then insert the custom field key and choose the type of taxonomy.My advice is to make a test with a blank new widget, just in case you have activated an option that could interfere with this specific test.
Let me know, please.
Don’t know what am I doing wrong, tried to replicate it on the fresh install without any plugins or widgets but now it doesn’t work at all.
See it here https://innpyt.com/
Getting Posts > Change the query when on single posts
– When on single posts, get posts from this custom field – Checked
– Get posts with this custom field key – _yoast_wpseo_primary_categoryThen I’ve created 4 categories: red, blue, greed and violet and assigned different posts to them. Each have a fetured image with corresponding color.
I can confirm that they all have _yoast_wpseo_primary_category meta_key in the _postmeta table with the category ID as a value.
But if I go to the single post – e.g
https://innpyt.com/test3/
https://innpyt.com/test16/
Sidebar pulls the posts based on their chronological order, despite of the ‘None’ in the ‘Order by’ in the ‘Getting posts’ menu.here are few screenshots https://imgur.com/a/9ZV9z
Which category do you want to display in the sidebar? Red? Green? Blu? Violet?
Also, tell me these informations:
1) Post “test16”:
categories =
custom field key =
custom field value =2) Post “test17”:
categories =
custom field key =
custom field value =I ask this because “test16” seems to display posts from category “Violet”, while “test17” not.
Also, please, activate the debugging options in the “Debugging” panel of the widget.
Let me know, when you’re done.
- The topic ‘Multiple Category order’ is closed to new replies.