WordPress.org

Support

Support » Plugins and Hacks » Hacks » tax_query and wp_query how-to?

tax_query and wp_query how-to?

  • So. I’m writing this section of a website that needs to display job opportunities that i’ve registered as a custom post type with a hierarchical taxonomy that indicates job levels.

    of course, by inserting this before the loop, i can get it to display only the custom posts marked with the “level1 and level2” taxonomy:

    $levelquery = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'vagas_tipo',
                'field' => 'slug',
                'terms' => array('level1','level2'),
    
           ),
        )
    );
    
    $jobquery = new WP_Query($levelquery);

    problem is: I don’t want to hardcode levels here, since the client might just use the WP interface to add more levels later on, and at the same time, i want to display them all in a single page, either as separate loops (one for each taxonomy term) or as a single loop ordered by taxonomies so my result would end up as:

    > level1
    <!– all the job-posts marked as level1 –>

    >level2
    <!– all the job-posts marked as level2 –>

    and so on.

    Can anyone point me to the light?

Viewing 3 replies - 1 through 3 (of 3 total)
  • ok, i just realized the existence of the hacks subforum. Can anyone move this topic there, please?

    [done]

    you people are fast! 🙂

    Ok, so i got this far:

    $terms = get_terms('vagas_tipo',
            array(
                'orderby' => 'slug',
                'order' => 'ASC',
                'hide_empty' => 1,
                'fields' => 'ids',
    
            ));
    
    $nivelquery = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'vagas_tipo',
                'field' => 'id',
                'terms' => $terms,
           ),
        ),
        'orderby' => 'meta_value',
        'meta_key' => 'salario',
        'order' => 'DESC'
    );
    
    $vagasquery = new WP_Query($nivelquery);

    WP_Query($nivelquery) and the loop will now print every post registered using my custom taxonomy in $terms, and order them by the meta_key ‘salario’.

    Problem now is: ‘meta_key’ doesn’t contain the terms registered with my custom taxonomy.

    So, i either need to add a new update_post_meta declaration when saving the post, to have a new meta_key display the same value as a the taxonomy being saved, or some other solution i can’t think of now.

    any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘tax_query and wp_query how-to?’ is closed to new replies.
Skip to toolbar