• Resolved younghusband

    (@younghusband)


    I have a site that does book reviews and author interviews. I would like to use Custom Fields to keep metadata, particularly BookTitle and AuthorName, since the actual post titles are different. Then I envision having a page somewhere that outputs all posts with the BookTitle key in alphabetical order based on the Custom Field value.

    The list would end up like:

    BookTitle – PostTitle(Linked to review) – PostDate

    or for interviews

    AuthorName – PostTitle(Linked to review) – PostDate

    Being able to integrate this with ELA would be a dream, but is far beyond my skill.

    Maybe this is possible with the_meta, but it doesn’t look like it.

    So far I have seen List posts based on custom values and Search Custom Fields, but I couldn’t find a list by Key. Can someone point me in the right direction?

Viewing 3 replies - 31 through 33 (of 33 total)
  • Okay, I’m trying to do something similar and this looks as it could be modified to work for me too.

    I’d like to use WordPress Pages to keep an archive of articles I’ve written in various newspapers. I managed to get Pages listed by simply changing in Geoff’s code post_status to ‘static’ (see https://www.mothugg.se/texts)

    However, now I’ve got a few problems:

    1) The links are wrong, on two out of three test posts: /525/ (wrong) /en-annan-artikel (wrong) and /texts/testartikel (correct). I put {$post->guid} in the link part, but can’t figure out why they get different URL:s.

    2) I’d like to display both Publication and Date Published. How can I get both custom values to appear?

    3) I’d like to sort the list by Date Published, but if I have both custom fields in the list, then I guess ‘ORDER BY meta_value’ won’t work.

    Hmm… for problem 1, I’d suspect that the guid was created for each post with different settings for a) Permalinks off and b) with a different directory structure for page posts 2 and 3.

    Are there separate WP installations at work here? I’m curious how you are getting all your posts to have a directory of /item/ and (for testartikel at least) you have a directory of /texts/ working.

    The guid field is a bit persnickety — the function get_permalink() actually appears to clean up any problems with the guid for a post in the database. But if we’re selecting guid directly from the database, that link needs to be perfect. One solution might be to update the post, which seems to rewrite the guid for you. And hopefully correct it. Check that post 525 has a post slug too. And verify the en-annan-artikel slug for that page post.

    For problem 2, you’ll need to make a separate query for extra Custom fields (unless there are methods I don’t know) but it seems to me that you can’t join the user and meta tables and select data from multiple rows in the meta table. But maybe there is an advanced method I don’t know. If you wanted to sort using both meta fields, that would be a problem but if it’s just one you should be okay.

    After foreach ($recentposts as $post) {
    Use: $pub_date = $wpdb->get_var("SELECT meta_value from $wpdb->postmeta WHERE meta_key='YourOtherKey' and ID = $post->ID"); And you should change YourOtherKey to the non-sorting key and then echo $pub_date, or whatever variable name you use in place of $pub_date, in the echo statement there, where you want the other meta value printed.

    Okay, thanks! You helped me solve problems (2) and (3) (although ID should be changed to post_id).

    As for problem (1), it seems that WordPress gives different permalinks depending on how I save the Pages!

    * If the Page does not yet have a title, it’s permalink is set as its numerical ID. And changing the slug only results in the other kind of wrong permalink.

    * If I instead enter the title, page parent, page template etc first, and then save the Page, it works fine and it is correctly permalinked as a subpage of /texts/.

    * It also seems that when I add the first custom field meta value and click “Add custom field”, the Page is simply saved and I end up on the Write Post page. However, when I edit the saved page, adding additional custom fields works like ‘save and continue editing’ (ie. I stay on the Edit Page page).

    This must be a bug, right?

    And yes, for my normal posts, i have added /item/ to the permalink structure.

Viewing 3 replies - 31 through 33 (of 33 total)
  • The topic ‘List posts by Key’ is closed to new replies.