1

i need your help :)

I've made this custom Mysql query on WordPress:

    $querystr = "
   SELECT  DISTINCT meta_value  FROM $wpdb->postmeta WHERE post_id IN(     
   SELECT DISTINCT wpostmeta.post_id
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id 
   AND wpostmeta.meta_key  = 'color' 
   AND wpostmeta.meta_value  = 'red' 
   AND wposts.post_type = 'post' ) AND meta_key ='size' 
   ORDER BY RAND()
   LIMIT 4
   ";

How can i ADD in the query search for "Category name" or "Category ID" ?

Cheers ;)

2

If I'm not missing something, the below should work for you. It is pretty much straight from the Codex: WP_Query - Custom Field Parameters. Do it like this:

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'color',
            'value' => 'red',
            'compare' => '='
        ),
        array(
            'key' => 'size',
            'compare' => 'EXISTS'
        )
    )
);
$query = new WP_Query( $args );

Additional resources on here:

  • YES, works very FINE ;) – Caponi Elia Jun 27 '14 at 10:56
  • Good :) Remember using WordPress' build-in functionality is almost always preferable, of course sometimes you reach the boundaries of that, then you have to switch to a (more) custom solution. @CaponiElia Don't forget to accept the answer. – Nicolai Jun 27 '14 at 11:12
-1

To ADD in the query search for "Category name" or "Category ID" you have to apply inner join with ( wp_term_relationships ).

Pls try this

$category_id is your category id

<?php 

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'color',
            'value' => 'red',
            'compare' => '='
        ),
        array(
            'key' => 'size',
            'compare' => 'EXISTS'
        )
    ),
    'tax_query' => array(
                            'relation' => 'AND',
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $category_id,
                    'operator'  => 'IN'
                )),
);
$query = new WP_Query( $args );

?>
  • 1
    Please extend your answer with a proper explanation. – Nicolai Jun 26 '14 at 14:00
  • 1
    I've try but with no results...TBI can u give an example with my code ? :) – Caponi Elia Jun 26 '14 at 14:40
  • Please specify your post type , category name and any other information that could help to solve this. – TBI Infotech Jul 2 '14 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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