WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to set a custom field value as php variable controling a wp_query? (4 posts)

  1. Toppas81
    Member
    Posted 3 years ago #

    Hello there,

    I have the following problem. I'm programming the page for a small publisher. They have a lot of artists under contract, each with different items. I want to display the items beneath the artists posts.

    I thought I give each artist item the same tag, for example "artistone". And then create a custom field – called "ItemTag" with the value "artistone" – in the artists post. then I want to use this value as the value tag via wp_query to call for the posts with these tags.

    It has to be as simple as possible, because the client is unable to do any coding ... So the client only has to set up the custom field and put in the name of the artists items tag to show his/her items at the bottom of the post.

    But I can't get the value of any custom field set as tag value. This is what I have so far:

    <?php
    
    $variable = get_post_meta($post->ID, 'ItemTag', true); 
    
    //The Query
    query_posts( 'tag=$variable' );
    
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		<?php include(TEMPLATEPATH . '/custom-mini-product.php'); ?>
    
    <?php endwhile; else:
    
    endif;
    
    //Reset Query
    wp_reset_query();
    
    ?>

    The "custom-mini-product.php" is just the post info in a separate file – because I need to use these multiple times ...

    Thanks for any help :)

  2. silafu
    Member
    Posted 3 years ago #

    Try this
    query_posts(array('category_name' => $variable));

  3. Toppas81
    Member
    Posted 3 years ago #

    Thx a lot silafu, works like a charme! :)

    For anyone interested in the whole code, here is how I implemented it:

    <?php
    
    $variable = get_post_meta($post->ID, 'ItemTag', true); 
    
    //The Query
    query_posts(array('tag' => $variable));
    
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		<?php include(TEMPLATEPATH . '/custom-mini-product.php'); ?>
    
    <?php endwhile; else:
    
    endif;
    
    //Reset Query
    wp_reset_query();
    
    ?>
  4. cinematic
    Member
    Posted 3 years ago #

    Just what I needed ... thanks so much!

Topic Closed

This topic has been closed to new replies.

About this Topic