Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get the title of the first custom post type with a specific term in a taxonomy.
But I'm not great at SQL so therefor not great with using $wpdb.

Here is my code:

$posts = $wpdb->get_results("
    SELECT ID, post_title 
    FROM $wpdb->posts 
    LEFT JOIN $wpdb->term_relationships
    LEFT JOIN $wpdb->term_taxonomy
    WHERE post_type = 'property'
    AND $wpdb->terms.name = 'Locked'
    AND $wpdb->term_taxonomy.taxonomy = 'status'
");
echo $posts[0]->post_title;

Any suggestions on how to get the title of the first custom post type of 'property' with the term 'Locked' in the taxonomy 'status'?

Update
This is how I attempted this using WP_Query:

<?php
    $args = array(
        'post_type' => 'property',
        'tax_query' => array( array(
            'taxonomy' => 'Status',
            'field' => 'slug',
            'terms' => $term
        ))
    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) :
        $loop->the_post();
        the_title();
        echo '<div class="entry-content">';
        echo get_the_post_thumbnail();
        the_content();
        echo '</div>';
    endwhile;
?>

where $term is "Locked".

What I really need is a way that I can query by multiple terms and taxonomies in an array or multiple arrays.
Any hints?

share|improve this question
3  
Why do you need SQL? Why can't you use WP_Query with taxonomy parameters? –  helgatheviking Jul 20 '13 at 3:47
    
So I had looked at using WP_Query and here is the code that I used: –  graphical_force Jul 20 '13 at 14:43
    
I had looked at using WP_Query and here is the code that I used:<?php $args = array( 'post_type' => 'property', 'tax_query' => array(array( 'taxonomy' => 'Status', 'field' => 'slug', 'terms' => $term ))); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); the_title(); echo '<div class="entry-content">'; echo get_the_post_thumbnail(); the_content(); echo '</div>'; endwhile;?>where $term is "Locked". What I really need is a way that I can query by multiple terms and taxonomies in an array or multiple arrays. Any hints? Thanks! –  graphical_force Jul 20 '13 at 14:55
    
Sorry for the mess, I'm not sure if that was best in a comment form or if there is a better way to reply with code. –  graphical_force Jul 20 '13 at 14:57
    
You can edit your question (link on the bottom left). I took the liberty of doing that for you in this case. –  Johannes Pille Jul 20 '13 at 15:47

1 Answer 1

up vote 0 down vote accepted

Any suggestions on how to get the title of the first custom post type of 'property' with the term 'Locked' in the taxonomy 'status'?

$args = array(
    'post_type' => 'property',
    'tax_query' => array(
        array(
            'taxonomy' => 'status',
            'field' => 'slug',
            'terms' => 'locked'
        )
    )
);
$your_query = new WP_Query( $args );

while ( $your_query->have_posts() ) {
    $your_query->the_post();
    $the_title = get_the_title(); // variable $the_title now holds your title
}

What I really need is a way that I can query by multiple terms and taxonomies.

$args = array(
    'post_type' => 'property',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'status',
            'field' => 'slug',
            'terms' => 'locked'
        ),
        array(
            'taxonomy' => 'color',
            'field' => 'slug',
            'terms' => 'blue'
        )
    )
);
$your_query = new WP_Query( $args );

Related Reading:
http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

share|improve this answer
    
Thanks! That seems to do the trick. –  graphical_force Jul 20 '13 at 21:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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