Home › Forums › Pro Support › Show posts in current category and specific tag
- This topic has 38 replies, 2 voices, and was last updated 3 years, 6 months ago by
Tom.
-
AuthorPosts
-
March 5, 2020 at 5:24 am #13741
hezuf123
ParticipantHi there,
I want to use WPSP to show only posts that are in the current category. I managed to do that by changing a snippet I found here in the forum:
<?php $terms = get_the_terms( get_the_ID(), 'category' ); if ( !empty( $terms ) ){ // get the first term $term = array_shift( $terms ); $term_slug = $term->slug; } wpsp_display( 1234, 'tax_term="' . $term_slug . '"' ); ?>
Now what I would like to add is that in addition to showing only the current category it should only include posts with a specific tag. How would I do that?
March 5, 2020 at 4:25 pm #13750hezuf123
ParticipantAdding to this:
Is there a way to make the above snippet prioritize subcategories or primarycategories (as you can select in GeneratePress)?Ideal would be:
Primary Category (if not available)-> Subcategory (if not available)-> Any Category
March 6, 2020 at 7:29 pm #13771Tom
KeymasterHow are you defining primary categories?
You can use any of the WP_Query parameters: https://developer.wordpress.org/reference/classes/wp_query/
$terms = get_the_terms( get_the_ID(), 'category' ); if ( !empty( $terms ) ){ // get the first term $term = array_shift( $terms ); $term_slug = $term->slug; } $args = array( 'tax_term' => $term_slug, ); wpsp_display( 1234, $args );
March 7, 2020 at 6:16 am #13777hezuf123
ParticipantSorry, I can’t get this to work. Assuming the tag I want to limit shown posts to is called “new”. How would I add that to the snippet?
How are you defining primary categories?
I assumed it’s from the GP theme, but it’s from the SEO plugin, so this is how one would access the primary category according to the dev:
// WordPress core:
$term_id = get_post_meta( $post_id, ‘_primary_term_’ . $taxonomy, true ); // int or false//= TSF 3.0+:
$term = the_seo_framework()->get_primary_term( $post_id, $taxonomy ); // WP_Term or false.
$term_id = the_seo_framework()->get_primary_term_id( $post_id, $taxonomy ); // intAs mentioned above ideally it would look for Primary Category (if not available)-> Subcategory (if not available)-> Any Category.
Looking at the WP_Query parameters I couldn’t find anything for subcategory specifically. Is there a workaround?
March 7, 2020 at 9:36 am #13783hezuf123
ParticipantAs mentioned above ideally it would look for Primary Category (if not available)-> Subcategory (if not available)-> Any Category.
I realized that this might have been unclear. It should look for these categories on the post that is currently displayed.
March 7, 2020 at 8:03 pm #13798Tom
KeymasterMaybe this will work? If so, we can look into including primary categories etc..
$terms = get_the_terms( get_the_ID(), 'category' ); if ( !empty( $terms ) ){ // get the first term $term = array_shift( $terms ); $term_slug = $term->slug; } $args = array( 'tax_term' => $term_slug, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( $term->slug ), ), array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => array( 'new' ), ), ), ); wpsp_display( 1234, $args );
March 8, 2020 at 5:18 am #13803hezuf123
ParticipantUnfortunately it doesn’t seem to work…
I use “Order by: Random” in case that makes a difference. I don’t necessarily need it to be random every time the page reloads, but I wouldn’t know how else to get a different selection of posts to show under each article. EDIT: Figured out using “No Order” instead of “Random” is probably what I want. However it still doesn’t limit the posts it shows to the ones with the “new” tag.
March 8, 2020 at 4:25 pm #13816hezuf123
ParticipantSeems like “No Order” is not what I thought it is.
I know this is getting a little out of hand, but is there a way to randomize shown posts based on the current post ID or something? So one page has a static random selection of posts that meet the criteria mentioned above and the selection is the same even after reload.
So it would do this
- Check current post for Primarycat > Subcat (Category with a parent) > Parentcat (In this order and work with the first that’s available)
- Show posts that are randomly selected based on a static but unique value, such as current post ID and
- are in the same category selected above
- have the “new” tag and are randomly selected based on a static value, such as current post ID.
Thanks for all your help Tom!
March 8, 2020 at 4:37 pm #13818hezuf123
ParticipantEDIT: won’t let me edit my old posts anymore… there was one condition mentioned twice.
- Check current post for Primarycat > Subcat (Category with a parent) > Parentcat (In this order and work with the first that’s available)
- Show posts that are randomly selected based on a static but unique value, such as current post ID and
- are in the same category selected above
- have the “new” tag
Thanks for all your help Tom!
March 10, 2020 at 5:55 pm #13869Tom
KeymasterHi there,
I’m not sure how to do the random selection part, unfortunately. It might be worth asking over on https://wordpress.stackexchange.com, as the question itself is related to WP_Query. It’s possible that someone has a working example they can share.
If they do, we can easily integrate the args with WPSP.
March 11, 2020 at 6:02 am #13886hezuf123
ParticipantThanks for your reply, I think I found an easier way to do what I need. I found a related post plugin that lets you export Post IDs:
$rel_posts = get_crp_posts_id(array ('postid' => $post_id, 'limit' => 4));
Is there a way to implement this into the snippet you posted earlier? It should give out 4 ID’s that WPSP could output, right? If that’s possible I wouldn’t need any of the other conditions.
March 12, 2020 at 8:03 pm #13926Tom
KeymasterYou could try this:
$rel_posts = get_crp_posts_id(array ('postid' => $post_id, 'limit' => 4)); $args = array( 'post__in' => $rel_posts, ); wpsp_display( 1234, $args );
March 13, 2020 at 7:40 am #13943hezuf123
ParticipantThis doesn’t work unfortunately.
$scores = get_crp_posts_id(); $rel_posts = array_column( (array) $scores, 'ID' ); print_r( $rel_posts )
Gives out:
Array ( [0] => 305512 [1] => 334934 [2] => 332645 [3] => 334067 )
So this part does seem to work.
But when I add
$args = array( 'post__in' => $rel_posts, ); wpsp_display( 1234, $args );
It just displays the latest posts with no filter. So it doesn’t seem to take the ID’s into account.
March 13, 2020 at 8:21 pm #13961Tom
KeymasterStrange. Just to debug, what if you do this?:
$args = array( 'post__in' => array( 305512, 334934, 332645, 334067 ), ); wpsp_display( 1234, $args );
March 14, 2020 at 6:30 am #13971hezuf123
ParticipantDoesn’t work either, just shows the latest posts. I created a new Post List to test and it’s the same issue.
-
AuthorPosts
- You must be logged in to reply to this topic.