WordPress.org

Ready to get started?Download WordPress

Forums

tax_query and wp_query how-to? (4 posts)

  1. Ricardo Moraleida
    Member
    Posted 3 years ago #

    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?

  2. Ricardo Moraleida
    Member
    Posted 3 years ago #

    ok, i just realized the existence of the hacks subforum. Can anyone move this topic there, please?

    [done]

  3. Ricardo Moraleida
    Member
    Posted 3 years ago #

    you people are fast! :)

  4. Ricardo Moraleida
    Member
    Posted 3 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic