-1

I tried to create a custom query, but no results, it throws me an error.

$args = array(
                'post_type' => 'product',
                'posts_per_page' => 1,
                'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => 23,
                            )
                )
            );
            $query = new WP_Query( $args );
            var_dump($query->have_posts());
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    echo the_title();
                }
            } else {
                echo 'error';
            }

            wp_reset_query();

I checked the documentation, but I don't know where's the problem.

The problem is the $query->have_posts() always returns false. The code is works in the header.php, but not works in functions php. I would like to know that, how to improve the code to use it in the functions.php

  • What is the "error" you are getting? – Rarst May 12 '14 at 11:01
  • 1
    You say that you there is a product with term 23 assigned to it. Is this product published? – Krzysiek Dróżdż May 12 '14 at 11:12
  • 1
    We got a comment explosion in this question - I had to put it on hold - because it totally is unclear what you are asking and what your problem is. Please take your time to phrase a detailed edit that explains the complete problem you are facing. Then we can reopen it. – kaiser May 12 '14 at 13:08
  • 1
    No. You just added to the question what previously was in the comments. And as you have seen, this is much too less to understand the problem you are facing. Lean back, make yourself some tea or coffee and then try to explain it to someone who sees and knows nothing of your setup. Be detailed, show lots of code, explain your setup. Else it will stay closed. – kaiser May 12 '14 at 13:34
  • 2
    Please add debugging information There is nothing in that query that should cause it to fail in functions.php and it does work exactly the same when I test it. Something else is going on that is not mentioned in the question. – s_ha_dum May 12 '14 at 14:09
0

SQL query forming is wrong

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND 0 = 1 AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 1

Here 1=1 AND 0 = 1

  • Unfortunately doesn't help – Roberti May 12 '14 at 11:35
  • can u provide link of front end or the exact sql query which you will get when u dump $query – shahpranaf May 12 '14 at 11:36
  • From the page you are refering to - "terms (int/string/array) - Taxonomy term(s)". – Pieter Goosen May 12 '14 at 11:40
  • ownaj.com/demo – Roberti May 12 '14 at 11:43
  • Btw I wrote the code to functions.php, is it problem? – Roberti May 12 '14 at 11:44

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.