Forums

meta_key in query_posts, it doesn't work anymore (7 posts)

  1. jobzesage
    Member
    Posted 1 year ago #

    Hi there,

    on our website we use custom fields to store the ID of Vimeo movies that are displayed on the page. We also us that field to display an archive of all our videos and to feature on the frontpage the latest video published.

    It used to work well, lately we updated WordPress, and now it doesn't seem to work. I'm not sure when it stopped working, but I know it used to work under 2.9, and it doesn't work anymore on 3.1.1. The queryposts looks like this for the archive :

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("meta_key=vimeo&showposts=5&paged=$paged"); ?>

    Nothing is displayed, if I remove the meta_key=vimeo argument, then all the posts are displayed.

    It seems like the part regarding custom fields in query_posts has disappear from WordPress Codex.

    What is the right way of doing this now ? Custom SQL ?!? It feels like a step backward...

    Thanks for your input !

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

  3. jobzesage
    Member
    Posted 1 year ago #

    Thanks for the link, I thought maybe WP_query would be better than query_posts. So I implemented an example from the Codex with WP_query, and I get the same results.

    This :
    $wp_query->query('showposts=5'.'&paged='.$paged);
    works,

    and this :
    $wp_query->query('meta_key=vimeo&showposts=5'.'&paged='.$paged);
    doesn't, it returns an empty list.

    When I look at my database, I find records with the meta_key vimeo.

    Why ?!? I don't get it. Any thoughts ??

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    the link was not to point you towards WP_Query() as query_posts() uses the same parameters; but to the deprecated and new custom field parameters of the query.

  5. jobzesage
    Member
    Posted 1 year ago #

    Oh, reading the doc, I saw :

    meta_key (string) - Custom field key. Deprecated as of Version 3.1 in favor of 'meta_query'.

    So I tried to use meta_query instead:

    $wp_query= null;
    $wp_query = new WP_Query();
    $arguments = array(
     	'showposts' => 5,
       	'paged' => $paged,
       	'meta_query' => array(array('key'=>'vimeo'))
    );
    $wp_query->query($arguments);
  6. jobzesage
    Member
    Posted 1 year ago #

    And it didn't work...

  7. jobzesage
    Member
    Posted 1 year ago #

    OK, I got it figured out, the really issue was somewhere else...

    Post types !

    I have created some custom post_types, without specifying them they were ignored ! If I wrote 'any', they were also ignored. I had to list them explicitly and then it worked.

    So eventually :

    $arguments = array(
    'showposts' => 30,
    'paged' => $paged,
    'meta_query' => array(array('key'=>'vimeo')),
    'post_type' => array('student-blog','school-news','teaching-sneak-peak','media-portefolio','media-blog')
    );

    Is that a bug ?!?

Topic Closed

This topic has been closed to new replies.

About this Topic