I have a simple taxonomy term page which lists posts connected to a specific term. I would inject some different (not connecting to that term) posts into that loop. I tried pre_get_posts action but no avail. When I put all IDs into post__in filter the loop only return posts connected that taxonomy term.

Do you have any idea how could I solve this issue?


Ok I find a solution. I use posts_where filter in pre_get_posts. With this filter I can remove the term related WHERE part from the default SQL query.

function custom_loops($query) {
if (is_tax( 'service_photo_location' )){
    $query->set( 'posts_per_page', 10);
    $query->set( 'post__in', $ids);
    $query->set( 'orderby', 'title');
    $query->set( 'order', 'ASC');
    add_filter( 'posts_where', function ( $where ) {
        $where = str_replace("AND ( wp_term_relationships.term_taxonomy_id IN (778) )", "", $where);
        return $where;
    }, 10, 2 );
remove_action( 'pre_get_posts', 'custom_loops' );
add_action('pre_get_posts', 'custom_loops');

Your Answer

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

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