0

I've created a function that searches images for a tag, uses them to create a loop and build an array of their parents' IDs.

Its in a pre_get_posts.

Its all working fine except I'm obviously overwriting the query's settings as the site no longer thinks its a taxonomy or archive page.

So the page title is wrong and more importantly so are the body classes. It's set the default blog body classes "blog app-data index-data home-data".

It should be "archive tax-location_features term-red term-35 app-data index-data archive-data taxonomy-data taxonomy-location_features-data taxonomy-location_features-red-data"

    // likely to need to tweak something here.
    global $wp_query;
    // How can i just edit the posts but leave the rest of the query behind?
    $matched_locations = query_posts($matched_parent_args); // loads right posts but body has wrong classes

    $matched_locations = get_posts($matched_parent_args); // works but don't know how to get them set as the postst to display

    $wp_query->current_feature = $queried_tag_slug; // used and works in loop fpr posts to load right image
    // wp_reset_query(); // this obviously undoes all of the work above but resets the body classes etc.

full function

add_filter('pre_get_posts', __NAMESPACE__ . '\\fd_media_tax_parent_posts');

function fd_media_tax_parent_posts($query, $args = [])
{
if ($query->is_tax('location_features') && $query->is_main_query() && !is_admin()) {

    $queried_tag_slug = $query->queried_object->slug;
    $items_with_features_args = [
        'post_type'   => ['attachment','location' ],
        'posts_per_page' => -1,
        'post_status' => ['inherit', 'publish'],
        'tax_query'   => array(
            array(
                'taxonomy' => 'location_features',
                'field'    => 'slug',
                'terms'    =>  $queried_tag_slug,
            ),
        ),
    ];
     
    $items_with_features = get_posts($items_with_features_args);

    $found_attachments         = [];
    $found_attachments_parents = [];
    foreach ($items_with_features as $item):
        $found_attachments[]         = $item->ID;
       
        if($item->post_parent!==0 && !in_array_r($item->post_parent,$found_attachments_parents)){
             $found_attachments_parents[] = $item->post_parent;
        } 
        if ($item->post_parent !== 0) {}
    endforeach;

    $found_ids = array_merge($found_attachments_parents, $found_attachments);

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $matched_parent_args = [
        'post_type' => ['location'],
        'post__in'  => $found_attachments_parents,
        'posts_per_page' => 20,
        'paged' => $paged
    ];

    global $wp_query;
    // How can i just edit the posts but leave the rest of the query behind?
    $matched_locations = query_posts($matched_parent_args); // loads right posts but body has wrong classes
    $matched_locations = get_posts($matched_parent_args); // works but don't know how to get them set as the postst to display

    $wp_query->current_feature = $queried_tag_slug; // used and works in loop fpr posts to load right image
    // wp_reset_query(); // this obviously undoes all of the work above but resets the body classes etc.
    // 
} elseif ($query->is_tax('location_type') && $query->is_main_query() && !is_admin()) {
    // print_r($wp_query);
}
return $query;
}

Is there a way I can replace the main queries posts or reset the main query info to what it was before I ran query_posts ?

thanks in advance. D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.