• Resolved Vox

    (@voxclamantis)


    I can’t seem to figure out how to construct the correct query

    I made a custom taxonomy named “spotlight” which then has five or so terms. I want to act upon the taxonomy when someone views one of the spotlight archives. I only care about its value being “true” not which spotlight is viewed. This works if viewing a spotlight, but throws an error if viewing any non-spotlight archive (Warning: Undefined array key “spotlight”). Right now I have it beneath other taxonomies so that other taxonomies exit before reaching it, but that is bad practice. How do I perform a correct query?

    add_action('pre_get_posts','tweak_archive_post_order')
    function tweak_archive_post_order($query)
    {if ($query->is_main_query() && is_archive() && $query->query_vars['category_name'] == 'skirted')
    {$query->set( 'posts_per_archive_page','-1');
    $query->set( 'orderby','manual_sort_order');
    $query->set( 'meta_key','manual_sort_order');
    $query->set( 'order','ASC');
    }

    [....]

    elseif ($query->is_main_query() && is_archive() && ($query->query_vars['spotlight']))
    {$query->set('posts_per_archive_page','-1');
    $query->set('orderby','date');
    $query->set('order','ASC');
    }
    }


    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author nwjames

    (@nwjames)

    @voxclamantis,

    Sorry for delay in responding; I only saw the question just now.

    Hopefully you have already resolved the issue, but if not. To determine the presence of the taxonomy in the query, you should do the test either as array_key_exists( 'spotlight', $query->query_vars ) or isset( $query->query_vars['spotlight'] ) It will not error if the taxonomy is not present in the query.

    I cannot say what is the best ordering. It will depend on what you want it to do if there is a query with several criteria, e.g. with a category of ‘skirted’ and with spotlight’ defined.

    Regards,

    Neil James

    Thread Starter Vox

    (@voxclamantis)

    Worked. Ty.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Writing a correct query?’ is closed to new replies.