WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Custom Field Query

[Resolved] Custom Field Query

  • Smrth
    Participant

    @smrth

    Is there other way around?
    query_posts(‘meta_key=letter&meta_value=D’); just wont do.. wont display the results as there aren’t any meta value. When i tried it on my other blogs it worked perfectly..
    Does any1 know any other way to do this?
    sry for my bad english 😛

Viewing 15 replies - 1 through 15 (of 19 total)
  • Smrth
    Participant

    @smrth

    k.. i tried with different themes on same blog, shit didnt work.. that means problem is in sql?

    Can you please explain more what you want to do? Examples will help.

    Smrth
    Participant

    @smrth

    ok.. here it goes.. you know as those big encyclopedias can have shown only post with first beginning letter. when you click on A, generates post list that post name starts with letter A, same with B and etc.. whole alphabet..
    so.. i created function that creates custom field automatically with key of “letter” and value is first letter of post title.

    $first_letter = substr(“$letter_title”, 0, 1);

    this part worked. then i created list of alphabet links and added at end of each link “?letter=A”,”?letter=B” etc.. after that i played with php..

    <?php $letter = $_POST['letter'];
    if(isset($letter)) {$posts = query_posts('meta_key=letter&meta_value=$letter');} else {$posts = query_posts($query_string . '&orderby=title&order=asc' . '&showposts=10');} ?>

    second else part worked but when $letter was set, post didnt shown..

    hope you will understand..

    You probably need to change $_POST to $_GET.

    Smrth
    Participant

    @smrth

    its $_GET*
    i didnt do copy-paste.. i made mistake when i wrote it in reply form..
    problem is not so simple.. anyway.. ty for reply

    Have you tried printing $letter to see if it is really set to the value you want?

    First check that $letter is getting the letter from the URL just by echoing it out in the page e.g:

    echo “<h1>$letter</h1>”;

    Then you can remove it once you confirm the variable contains the letter properly.

    Secondly I think PHP variables will only work in double quotes not single quotes e.g you have:

    $posts = query_posts('meta_key=letter&meta_value=$letter');

    Try:

    $posts = query_posts("meta_key=letter&meta_value=$letter");

    Or

    $posts = query_posts('meta_key=letter&meta_value='.$letter);

    Smrth
    Participant

    @smrth

    in code its in double quotes.. yes i did it.. $letter have a value of one letter thats selected..

    Smrth
    Participant

    @smrth

    and i disabled all plugins to see if one of them had buged this, but still didnt work, also changing theme didnt work..

    So if you remove you query so you return all posts and then add in the loop:

    `$let = get_post_meta($post->ID, ‘letter’);
    var_dump($let);

    If nothing is printing then it means nothing is set for that post with the post meta key you have supplied.

    $let = get_post_meta($post->ID, ‘letter’, false);

    Smrth
    Participant

    @smrth

    here is the result “array(1) { [0]=> string(1) “D” }”

    $let = get_post_meta($post->ID, 'letter', true);
    query_posts('meta_key=letter&meta_value='.$let);

    try this now.

    So based on the above a page with the URL:

    mysite.com/pagename/?letter=A

    and the template for pagename as follows:

    <?php
    if($_GET['letter']){
    $letter = $_GET['letter'];
    $posts = query_posts("meta_key=letter&meta_value=$letter");
    }
    else{
    $posts = query_posts($query_string . '&orderby=title&order=asc' . '&showposts=10');
    }
    //Do stuff with $posts here
    ?>

    Should display the expected results?

    Smrth
    Participant

    @smrth

    @chinmoy29 its not working.. wont show posts..

    @xdesi yea.. it shows on other blogs but on this one i need, dont work..

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘[Resolved] Custom Field Query’ is closed to new replies.