Forums

[resolved] Define query_posts from Custom fields!!! (9 posts)

  1. ShirouJune
    Member
    Posted 1 month ago #

    Hi! I've been struggling with this for a while now... wonder if this can be done...

    This is my query_posts based on the post-slug.

    <?php query_posts('name=my-post-title-name'); ?>

    What I would like is... to define the "my-post-title-name" from the custom fields when writing a new post. Thought it could use a variable or something... not sure...

    I call a custom field like this...

    <?php if ( get_post_meta($post->ID, 'mykey', true) ) { ?>
             <?php _e("My value: "); echo get_post_meta($post->ID, "mykey", $single=true); } ?>

    Any ideas pretty please?...
    Thanks...

  2. MichaelH
    moderator
    Posted 1 month ago #

    Are you asking how to return a query of your posts, where the custom field value is equal to a value?

    If so it would be something like:

    query_posts('meta_key=mykey&meta_value=my-post-title-name');
  3. ShirouJune
    Member
    Posted 1 month ago #

    No... I meant... the function is

    query_posts('name=post-slug');

    so name= should remain there, while post-slug should be defined by my custom field key that is equal to a value...

    I am clueless on php... so forgive the raw example...

    but it should be something like:

    query_posts('name=$mypostslug');

    where

    $mypostslug =
    
    <?php if ( get_post_meta($post->ID, 'mykeyslug', true) ) { ?>
    <?php _e("My value: "); echo get_post_meta($post->ID, "mykeyslug", $single=true); } ?>

    something like that... though I know the $ cannot be readable because of the single quotes...

  4. MichaelH
    moderator
    Posted 1 month ago #

    Might review the available arguments for query_posts. See Template_Tags/query_posts

  5. ShirouJune
    Member
    Posted 1 month ago #

    Already did... it was from there that I picked:

    Post & Page Parameters

    Retrieve a single post or page.

    * 'p' => 27 - use the post ID to show that post
    * 'name' => 'about-my-life' - query for a particular post that has this Post Slug

    here: http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters

    It works... I just need to find how to retrieve that value from a custom field...

  6. MichaelH
    moderator
    Posted 1 month ago #

  7. ShirouJune
    Member
    Posted 1 month ago #

    Did too. I believe it's a programming issue... I saw somewhere a code to store information in a variable that could be used later but can't find it...

  8. ShirouJune
    Member
    Posted 1 month ago #

    Found my way through it. It was easier than thought...

    <?php $mydiscslug = get_post_meta($post->ID, "disc", $single=true); ?>
    
    <?php query_posts('name=' . $mydiscslug); ?>

    These two lines made the trick. Some conditionals... and it works like a charm: http://www.siamshade-everlasting.com/en/lyrics/binecks_lyrics/2009/kimi-ga-tooku-te/

    Used to retrieve all the content in the "Song Info" stored in another post.

    Hope it helps someone outhere!!!

    Regards!

  9. podda999
    Member
    Posted 2 weeks ago #

    Hey could this be edited to work for the following. I dont know PHP that well so here is what i have.

    I want to move the content from bosts which have a custom field of embed and put the data within description. I was going to do this in SQL but is there is a more simple way, this would be great.

    So what i want to do. Post with meta_value = 'embed' i want to move this the the description field within my post.

    Any help would be great thanks.

Reply

You must log in to post.

About this Topic