Support » Fixing WordPress » get_posts() With Custom Meta Tag

  • questions4wp

    (@questions4wordpress)


    I have a quick question about the get_posts() function – I can’t seem to get it to return anything.

    I created a custom meta tag for a page and tagged it as “MetaTag”. In the page’s PHP, I first grab the meta information and display it (works fine), and then I try to grab pages based on them matching that meta information, and I can’t seem to get that to work.

    What I have is the following:

    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    	echo "Current page's meta tags: ".$my_meta['name']."<br />";
    
    $args = array(
    	'meta_query' => array(
    		array(
    			'name' => 'MetaTag',
    		)
    	)
     );
    
    $postslist = get_posts( $args );
    	echo "Post list: ".$postlist;

    The first echo command yields: “Current page’s meta tags: MetaTag”, as it should. However, the second echo command yields “Post list: “, but the actual return of $postlist is blank. My goal is to get it to say “Post list: 89”, i.e. return the post ID’s of the posts who have the correct meta tag. I assume this is just a basic misuse of the get_posts() function, but I’m not sure where I’m going wrong.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter questions4wp

    (@questions4wordpress)

    I have a little more information, if this helps answer my question. I added the following code, just to see the results:

    $all_metadata = get_post_custom($post->ID);
    	echo "All metadata: ";
    	print_r($all_metadata);
    	echo "<br />";

    And I got the following back:
    All metadata: Array ( [_edit_last] => Array ( [0] => 4 ) [_edit_lock] => Array ( [0] => 1337777823:4 ) [_wp_page_template] => Array ( [0] => sidebar-page.php ) [_my_meta] => Array ( [0] => a:2:{s:4:”name”;s:7:”MetaTag”;s:11:”description”;s:11:”Description”;} ) )

    So basically, I want to use get_posts() properly to return all posts with certain meta information, which I know it has because get_post_custom() displays it. I just can’t figure out the syntax for get_posts().

    Thread Starter questions4wp

    (@questions4wordpress)

    More information: If I run

    print_r(get_post_custom_keys($post->ID));

    I get

    ( [0] => _edit_last [1] => _edit_lock [2] => _wp_page_template [3] => _my_meta )

    so it looks like my custom key is “_my_meta”. However, if I then run

    $query = new WP_Query( 'meta_key=_my_meta' );

    I get nothing. Sigh any advice?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_posts() With Custom Meta Tag’ is closed to new replies.