WordPress.org

Support

Support » WP-Advanced » Passing Variable to wpostmeta.meta_value

Passing Variable to wpostmeta.meta_value

  • IslandGirl
    Member

    @islandgirl

    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: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

Viewing 2 replies - 1 through 2 (of 2 total)
  • IslandGirl
    Member

    @islandgirl

    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.

    <?PHP
    $thehotel = $post->ID;
    ?>

    <?php

    $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.

    Chris

    designsimply
    Member

    @designsimply

    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.

    <?php
    get_post_meta($post->ID,'special_location',TRUE);
    ?>

    http://codex.wordpress.org/Using_Custom_Fields#Advanced_Techniques_for_Custom_Fields

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