1

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?

5
  • 3
    Why do you need SQL? Why can't you use WP_Query with taxonomy parameters? Commented Jul 20, 2013 at 3:47
  • So I had looked at using WP_Query and here is the code that I used: Commented Jul 20, 2013 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! Commented Jul 20, 2013 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. Commented Jul 20, 2013 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. Commented Jul 20, 2013 at 15:47

1 Answer 1

1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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