[Support request] Show posts in current category and specific tag

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Show posts in current category and specific tag

This topic contains 38 replies, has 2 voices, and was last updated by  Tom 3 days ago.

Viewing 15 posts - 1 through 15 (of 39 total)
  • Author
    Posts
  • #13741

    hezuf123
    Participant

    Hi 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?

    #13750

    hezuf123
    Participant

    Adding 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

    • This reply was modified 3 weeks, 2 days ago by  hezuf123.
    #13771

    Tom
    Keymaster

    How 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 );
    #13777

    hezuf123
    Participant

    Sorry, 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 ); // int

    As 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?

    • This reply was modified 3 weeks ago by  hezuf123.
    • This reply was modified 3 weeks ago by  hezuf123.
    #13783

    hezuf123
    Participant

    As 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.

    #13798

    Tom
    Keymaster

    Maybe 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 );
    #13803

    hezuf123
    Participant

    Unfortunately 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.

    • This reply was modified 2 weeks, 6 days ago by  hezuf123.
    #13816

    hezuf123
    Participant

    Seems 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

    1. Check current post for Primarycat > Subcat (Category with a parent) > Parentcat (In this order and work with the first that’s available)
    2. Show posts that are randomly selected based on a static but unique value, such as current post ID and
      1. are in the same category selected above
      2. have the “new” tag and are randomly selected based on a static value, such as current post ID.

    Thanks for all your help Tom!

    #13818

    hezuf123
    Participant

    EDIT: won’t let me edit my old posts anymore… there was one condition mentioned twice.

    1. Check current post for Primarycat > Subcat (Category with a parent) > Parentcat (In this order and work with the first that’s available)
    2. Show posts that are randomly selected based on a static but unique value, such as current post ID and
      1. are in the same category selected above
      2. have the “new” tag

    Thanks for all your help Tom!

    #13869

    Tom
    Keymaster

    Hi 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.

    #13886

    hezuf123
    Participant

    Thanks 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.

    • This reply was modified 2 weeks, 3 days ago by  hezuf123.
    • This reply was modified 2 weeks, 3 days ago by  hezuf123.
    • This reply was modified 2 weeks, 3 days ago by  hezuf123.
    • This reply was modified 2 weeks, 3 days ago by  hezuf123.
    #13926

    Tom
    Keymaster

    You 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 );
    #13943

    hezuf123
    Participant

    This 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.

    • This reply was modified 2 weeks, 1 day ago by  hezuf123.
    #13961

    Tom
    Keymaster

    Strange. Just to debug, what if you do this?:

    $args = array(
        'post__in' => array( 305512, 334934, 332645, 334067 ),
    );
    
    wpsp_display( 1234, $args );
    #13971

    hezuf123
    Participant

    Doesn’t work either, just shows the latest posts. I created a new Post List to test and it’s the same issue.

Viewing 15 posts - 1 through 15 (of 39 total)

You must be logged in to reply to this topic.