WordPress.org

Ready to get started?Download WordPress

Forums

how to use tax_query in get_posts()? (5 posts)

  1. wendallsan
    Member
    Posted 1 year ago #

    Hi all,

    I'm trying to write a query that will get all posts of a specific post type and with a specific value in the post's 'speaker' meta field. I'm sure I have a post with the matching speaker id, and have illustrated it below by first iterating through the posts and putting the speaker id meta value, then querying again with that specific meta value. I'm not getting any results as soon as I add a value to my meta_query array.

    $args = array( # GET ALL CHAPTERED_VIDEO POSTS WITH A META KEY OF 'SPEAKER'
                'posts_per_page' => -1,
                'post_type' => 'chaptered_video',
                'meta_query' => array(
                        array(
                                'key'=>'speaker'
                        )
                )
        );
    
        $posts = get_posts($args); # GET THE POSTS
    
        foreach($posts as $post){ # LOOP THRU POSTS
                $meta = get_post_meta($post->ID, 'speaker', true); # GET THE POST'S 'SPEAKER' META
                echo "post '$post->post_name' speaker meta:<br/>"; # OUTPUT THE META
                print_r($meta);
                echo "<br/><br/>";
        }
    
        $args = array( # GET ALL CHAPTERED_VIDEO POSTS WITH A META KEY OF 'SPEAKER' AND A META VALUE OF 145
                'posts_per_page' => -1,
                'post_type' => 'chaptered_video',
                'meta_query' => array(
                        array(
                                'key'=>'speaker',
                                'value'=> 145 # THIS IS THE 1ST VALUE OUTPUT IN THE LOOP ABOVE
                        )
                )
        );
    
        $posts = get_posts($args); # GET THE POSTS
        if (empty($posts)) echo "there are no posts<br/>"; # OUTPUT IF EMPTY
        else { # ELSE SHOW THE POSTS
                echo "posts: <br/>";
                print_r($posts);
                echo "<br/><br/>";
                $posts_count = count($posts);
                echo "there are $post_count posts.<br/>";
        }
    
        /* THE ABOVE OUTPUTS:
        post 'ancient-traditions' speaker meta:
        Array ( [0] => 145 ) 
    
        post 'ancient-traditions-3' speaker meta:
        Array ( [0] => 151 )
    
        post 'ancient-traditions-2' speaker meta:
        Array ( [0] => 156 )
    
        post 'ancient-traditions-1' speaker meta:
        Array ( [0] => 158 )
    
        there are no posts
        */

    You can see that the above loops through all posts of the 'chaptered-video' post type and outputs the post's speaker meta, which is the post ID of another post type on the site. But then if I specify one of the values output by the 1st query, I don't get any results when I think I should be getting one result. Can anyone explain what I'm doing wrong? Thanks for any help.

  2. bcworkz
    Member
    Posted 1 year ago #

    I'm not experienced with meta_query, but I wonder if the problem is something silly like data type. Could the meta data be stored as the string "145" instead of integer 145? So just specifying 'value'=>'145' would get the result?

    Top tip: When there's a possible issue with type, use var_dump() instead of print_r().

  3. wendallsan
    Member
    Posted 1 year ago #

    bcworkz, you hit the head on the nail. thank you. Changing my print_r to var_dump indeed indicates that the meta data is stored as a string rather than an integer. However, changing my query's value from an integer to a string still does not return any results. Here's my modified code and output:

    $args = array( # GET ALL CHAPTERED_VIDEO POSTS WITH A META KEY OF 'SPEAKER'
        'posts_per_page' => -1,
        'post_type' => 'chaptered_video',
        'meta_query' => array(
            array(
                'key'=>'speaker'
            )
        )
    );
    
    $posts = get_posts($args); # GET THE POSTS
    
    foreach($posts as $post){ # LOOP THRU POSTS
        $meta = get_post_meta($post->ID, 'speaker', true); # GET THE POST'S 'SPEAKER' META
        echo "post '$post->post_name' speaker meta:<br/>"; # OUTPUT THE META
        var_dump($meta);
        echo "<br/><br/>";
    }
    
    $args = array( # GET ALL CHAPTERED_VIDEO POSTS WITH A META KEY OF 'SPEAKER' AND A META VALUE OF 145
        'posts_per_page' => -1,
        'post_type' => 'chaptered_video',
        'meta_query' => array(
            array(
                'key'=>'speaker',
                'value'=> "145" # THIS IS THE 1ST VALUE OUTPUT IN THE LOOP ABOVE
            )
        )
    );
    
    $posts = get_posts($args); # GET THE POSTS
    if (empty($posts)) echo "there are no posts<br/>"; # OUTPUT IF EMPTY
    else { # ELSE SHOW THE POSTS
        echo "posts: <br/>";
        var_dump($posts);
        echo "<br/><br/>";
        $posts_count = count($posts);
        echo "there are $post_count posts.<br/>";
    }

    This outputs:

    post 'who-do-you-say-that-i-am-session-1-expectation-of-a-messiah' speaker meta:
    array(1) { [0]=> string(3) "145" }
    
    post 'ancient-traditions-from-the-hebrew-scriptures-session-3' speaker meta:
    array(1) { [0]=> string(3) "151" }
    
    post 'ancient-traditions-from-the-hebrew-scriptures-session-2' speaker meta:
    array(1) { [0]=> string(3) "156" }
    
    post 'ancient-traditions-from-the-hebrew-scriptures-session-1' speaker meta:
    array(1) { [0]=> string(3) "158" }
    
    post 'ancient-traditions' speaker meta:
    array(1) { [0]=> string(3) "145" }
    
    there are no posts

    How can I get the posts? I figured that changing the value to the proper data type would work, but I'm still not getting any results back from my second get_posts() call.

  4. bcworkz
    Member
    Posted 1 year ago #

    Aaaargh! I was was afraid the type mismatch was not the real problem, as PHP usually deals with that without issue. I really don't see what could be the problem now. Unless someone else notices something, all I can suggest is try various compare arguments as well. Maybe just explicitly 'compare'=>'=' (shouldn't it be '==' or '==='?), documentation says it defaults to '=', maybe that's the issue? Except multi-equals do not appear to be a valid option. I dunno, like I said, I don't have experience with this particular argument set. Or maybe try 'compare'=>'LIKE'? Or try placing "145" into any array and use the 'compare'=>'IN'?? Sorry, I'm grasping at straws. Hope you find something that works, good luck.

  5. wendallsan
    Member
    Posted 1 year ago #

    Figured it out . . . I was using a plugin called Custom Fields to add fields to my custom post type, which by default serializes everything, even fields that allow for only one value. You can't use the meta_query with a serialized value, unless you search for the entire (unserialized) string. I fixed this by implementing the field in question manually in the custom post type rather than relying on the plugin for this field. Thanks very much for your input bcworkz!

Topic Closed

This topic has been closed to new replies.

About this Topic