Site logo

Archived topic

Elements not showing up on archives pages

7 replies · Started by C on August 14, 2020

Viewing posts 1–8 of 8

Hi,
I'm using a custom function to reorder posts on my category pages based on a custom field. However, this causes elements - both hooks and headers - to not show up on these pages. The green banner on the site is a header element set to be displayed on the entire site but does not show up on my category pages. I realize this isn't exactly a GP issue but I'm wondering if there is any way for me to continue using the function and display elements on these pages:

function order_by_custom_field( $query ) {
    if ( is_category()) {
$query->set('meta_query', array(
    array(
    'meta_key' => 'custom_key',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
    )
));
$query->set('meta_key', 'custom_key');
$query->set('orderby', 'meta_value_num');
    }
}
add_action( 'pre_get_posts', 'order_by_custom_field' );

Hi there,

Can't see how the two are related.

How are you adding the function?

And what's inside the page hero content?

The function was added to functions.php and the page hero content contains a styled div.

And the element shows up correctly as soon as you remove that function?

What's inside the styled <div>?

At the time, I had an ad dynamically inserted inside the styled div. It shows up perfectly on all pages except the pages affected by my function. I'm not a wordpress developer but from what I've read, it seems to be related to pre_get_posts modifying the page.

The stack exchange question below was the closest thing I could find that best describes the problem from my basic understanding, but the solution is way over my head.

https://wordpress.stackexchange.com/questions/214895/using-pre-get-posts-on-true-pages-and-static-front-pages

And yes, once the function is removed, everything shows up as it should.

Finally got it working. All I had to do was add "&& $query->is_main_query()" to the conditional.

Glad to hear!

This archived topic is closed to new replies.