0
$topics = query_posts( array( 
      'post_type' => 'topics', 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'meta_query' => array(
             'key' => 'forum_category', 
             'value' => $forum_id, 
             'compare'   => '=' 
             ) 
      ) 
 );

Doesn't seem to make a difference to this query what you set $forum_id to, it just loads all topics for all forum_category inputs... Any ideas what could cause this?

Just to be clear, this is a function being called in the loop, so where the function is being called $forum_id is set by get_the_id()

  • use var_dump(get_the_id()) its the number different? – David Lee Sep 29 '17 at 20:05
  • Yes, tested that and the number is different. – lukgoh Sep 29 '17 at 20:08
  • Have you tried using different operators? Such as LIKE or IN. By the way, you should use WP_Query() instead of query_posts(). – Jack Johansson Sep 29 '17 at 20:16
  • Why would I use WP_Query? – lukgoh Sep 29 '17 at 20:17
  • query_posts will override the main query. Have a look at this. – Jack Johansson Sep 29 '17 at 20:18
2

You can pass your meta query into query_posts or (the preferable) WP_Query in 2 ways:

  1. An array using the array keys meta_key, meta_value, meta_type and meta_compare
  2. An array of arrays using the array keys key, value, type and compare

In your question, you were trying to use a mix of the two, and that's why it wasn't working.


1. An array using the array keys meta_key, meta_value, meta_type and meta_compare

This is the way you were trying to do it in your question, but the meta_query should have been using the following array keys:

$args = array( 
      'post_type' => 'topics', 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'meta_query' => array(
             'meta_key' => 'forum_category', 
             'meta_value' => $forum_id, 
             'meta_compare'   => '=' 
             ) 
      ) 
 );


2. An array of arrays using the array keys key, value, type and compare

From the Codex for WP_Query:

Important Note: meta_query takes an array of meta query arguments arrays (it takes an array of arrays) - you can see this in the examples below. This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries.

$args = array( 
      'post_type' => 'topics', 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'meta_query' => array(
          array(
             'key' => 'forum_category', 
             'value' => $forum_id, 
             'compare'   => '=' 
             ) 
          )
      ) 
 );

The advantage of using this way is that you can add multiple meta_queries to refine the results, for example you could get all posts where both forum_category = $forum_id AND forum_moderator = $moderator_name

$args = array( 
      'post_type' => 'topics', 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'meta_query' => array(
          'relation' => 'AND',  // this could also be "OR"
          array(
             'key' => 'forum_category', 
             'value' => $forum_id, 
             'compare'   => '=' 
             ),
          array(
            'key'     => 'forum_moderator',
            'value'   => $moderator_name,
            'compare' => '='
        ),
          )
      ) 
 );


Ref: See the Codex for WP_Query for more information and examples.

Note: I realise you found an answer, but I thought the additional information and alternative usage might be helpful - even if not to you, then to other users searching with a similar problem.

  • Thank you. My problem was for the function I was writing I couldn't do it inside the usual loops so I couldn't figure a decent way around using the method I went with. – lukgoh Oct 1 '17 at 9:07
  • 1
    @lukgoh No problem. And it turns out that all you'd wrong was the array key names! That's why I thought I'd post a full answer here, just in case anyone else runs into the same problem - its so simple to get them mixed up :) – FluffyKitten Oct 1 '17 at 9:20
1

Changing the query to:

 $topics = query_posts( 
            array( 
                'post_type' => 'topics',
                'meta_query'  => array( 
                    array( 
                        'key' => 'forum_category', 
                        'value' => $forum_id 
                    ) 
                ) 
            ) 
        );

fixed it, I guess the meta query had to be in a double array? I can't see any difference apart from that...

  • 1
    Yes, meta_query accepts those query arguments in an array - to pass then in directly you can use meta_key, meta_value, meta_type and meta_compare instead of array(key, value, type, compare). See examples in the WP_Query Codex – FluffyKitten Oct 1 '17 at 1:43

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.