WordPress.org

Forums

[resolved] Custom Field Query (20 posts)

  1. Jaroslav Suhanek
    Member
    Posted 4 years ago #

    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 :P

  2. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

  3. vtxyzzy
    Member
    Posted 4 years ago #

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

  4. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

  5. vtxyzzy
    Member
    Posted 4 years ago #

    You probably need to change $_POST to $_GET.

  6. Jaroslav Suhanek
    Member
    Posted 4 years ago #

    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

  7. vtxyzzy
    Member
    Posted 4 years ago #

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

  8. xdesi
    Member
    Posted 4 years ago #

    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);

  9. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

  10. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

  11. xdesi
    Member
    Posted 4 years ago #

    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.

  12. chinmoy29
    Member
    Posted 4 years ago #

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

  13. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

  14. chinmoy29
    Member
    Posted 4 years ago #

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

    try this now.

  15. xdesi
    Member
    Posted 4 years ago #

    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?

  16. Jaroslav Suhanek
    Member
    Posted 4 years ago #

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

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

  17. xdesi
    Member
    Posted 4 years ago #

    What does:

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

    display?

  18. Jaroslav Suhanek
    Member
    Posted 4 years ago #

    is $letter is set, it displays nothing, if is not set display usual thing, posts in that category (as it should work)..

  19. Jaroslav Suhanek
    Member
    Posted 4 years ago #

    i give up on this.. its not worthy xD

    edit: i wanted to check once more and disabled all plugins and activate one by one.. at the end plugin called Advanced Category Excluder was making problems..

    sry that i wasted your time and thanks for all help!

  20. xdesi
    Member
    Posted 4 years ago #

    oops :D

Topic Closed

This topic has been closed to new replies.

About this Topic