0

I am working on dailysuit.de, which features four featured posts at the top, defined by a certain tag. The order is newest to oldest.

How do I get this order randomised (and not newest to oldest)?

I tried to change something like the following in inc/featured-content.php:

// Query for featured posts.
$featured = get_posts( array(
        'numberposts' => $settings['quantity'],
        'tax_query'   => array(
            array(
                'field'    => 'term_id',
                'taxonomy' => 'post_tag',
                'terms'    => $tag,
            ),
        ),
    ) );

to

// Query for featured posts.
$featured = get_posts( array(
        'numberposts' => $settings['quantity'],
        'tax_query'   => array(
            array(
                'field'    => 'term_id',
                'taxonomy' => 'post_tag',
                'terms'    => $tag,
            ),
        ),
        'orderby'          => 'rand',
    ) );

but there are two flaws:

1. It did not work/randomise.

2. Changing this file is a bad idea. If the theme gets an update, the randomising is gone.

Is there a working solution having it in a child theme? Thanks a lot in advance.

1 Answer 1

2

Third party themes are off topic, but I ended up checking it anyway:

The Problem

What comes before & after your posted code is important:

Before:

// Return array of cached results if they exist.
$featured_ids = get_transient( 'featured_content_ids' );
if ( ! empty( $featured_ids ) ) {
    return array_map( 'absint', (array) $featured_ids );
}

After:

// Ensure correct format before save/return.
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
$featured_ids = array_map( 'absint', $featured_ids );
set_transient( 'featured_content_ids', $featured_ids );

This means your theme is caching the featured post IDs, through the Transients API.

So randomizing will not work like you expected.

The default TTL is 0, meaning it will never expire, until it's deleted by hand.

Workarounds

Here are four different methods:

Method 1)

Instead, modify this in Featured_Content::get_featured_posts():

$featured_posts = get_posts( array(
        'include'        => $post_ids,
        'posts_per_page' => count( $post_ids ),
) );

to:

$featured_posts = get_posts( array(
        'include'        => $post_ids,
        'posts_per_page' => count( $post_ids ),
        'orderby'        => 'rand'
) );

within your child theme.

Method 2)

We could also shuffle the posts with the longform_get_featured_posts filter:

add_filter( 'longform_get_featured_posts', function( $posts )
{
    shuffle( $posts );
    return $posts;
}, 11 );

Method 3)

We could modify the orderby within the pre_get_posts hook:

add_action( 'pre_get_posts', function( $q )
{
    if( 
           did_action( 'longform_featured_posts_before' ) 
        && ! did_action( 'longform_featured_posts_after' ) 
    )
        $q->set( 'orderby', 'rand' );            
}, 99 );

between the longform_featured_posts_before and longform_featured_posts_after hooks.

Method 4)

We could try to increase the theme's quantity option (from the backend) to 6 but display only 4 (just as an example). We could then use a modification of method #2:

add_filter( 'longform_get_featured_posts', function( $posts )
{
    shuffle( $posts );

    if( count( $posts ) > 4  )
        $posts = array_slice( $posts, 0, 4 );

    return $posts;
}, 11 );

To increase the number above 6, then max_posts need to be modified from this code part:

// Add support for featured content.
add_theme_support( 'featured-content', array(
    'featured_content_filter' => 'longform_get_featured_posts',
    'max_posts' => 6,
) );

Many other variations could be possible, but I stop here ;-)

Note that I've never installed or used this theme before. These are just some ideas based on skimming through the theme's source code.

11
  • Thank you very much for helping, but I am not so sure, where to find "Featured_Content::get_featured_posts():". In the child theme there was no file having that code inserted.
    – vega
    Commented Aug 7, 2015 at 18:31
  • Okay, I found that line in inc/featured-content.php within the original/mother theme. If I change the code there, it will be gone on the next update. If I duplicate this directory and file to the child theme, it does not work, I believe. I tried it last week with a different issue.
    – vega
    Commented Aug 7, 2015 at 18:39
  • I updated the answer with another (untested) option, that you could add to your functions.php file, to test @vega
    – birgire
    Commented Aug 7, 2015 at 18:44
  • Nice idea. I tried that, but does not work, it seems, after testing with STRG + F5.
    – vega
    Commented Aug 7, 2015 at 18:55
  • I modified the priority for the second method @vega
    – birgire
    Commented Aug 7, 2015 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.