Support » Developing with WordPress » Passing Variable to wpostmeta.meta_value

  • I have a site that is setup with Pages and posts. I have a custom field in the posts that has the numerical value (e.g., 297) of the Page entered depending on the hotel that I need to retrieve the posts for (the posts are specials that are running for that hotel).

    I need to be able to pass a variable to the wpostmeta.meta_value in the page template used just for the specials.

    Using the select query from here:

Viewing 2 replies - 1 through 2 (of 2 total)
  • Explanation: I have a Page template called “Special Promotions” and each hotel has its own subpage for their promotions.

    The reasoning behind the various promotions is that someone can select just the type of promotion in case they are looking for Romantic, Ski, Family, etc.

    You do need to add a custom field to the posts (mine is special_locations with the page # entered in the value field.

    $thehotel = $post->ID;


    $querystr = “
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = ‘special_location’
    AND wpostmeta.meta_value = ‘$thehotel’
    AND wposts.post_status = ‘publish’
    AND wposts.post_type = ‘post’
    AND wposts.post_date < NOW()
    ORDER BY wposts.post_date DESC

    $pageposts = $wpdb->get_results($querystr, OBJECT);


    Then all of the rest of the code from the codex.





    Are you using a custom query select because you need to query for custom fields for related posts even though you’re on a page?

    I think you could do it like this if you want to get the custom field value for a post that you are on.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Passing Variable to wpostmeta.meta_value’ is closed to new replies.