Home Forums Backend Issues (wp-admin) DB query for field via URL


DB query for field via URL

  • This is asking a bit so I’ll understand if no one wants to answer this but I felt it was worth the risk:

    I’m new to shortcode creation (I have a basic one I wrote to place graphic stars within pages for our movie reviews) and had an idea on something to do next that I need some input on.

    As mentioned, we do reviews and provide stars for the movies that are in a custom field we added with ACF (called Rating) which stores a numeric from 0 to 5. The problem is that we have each rating shown in 3 places — the review, a sidebar for a list of the most current reviews and a page for the compendium of all reviews for the current year. The issue is that each are manual so we’re introducing bad data every so often. For example a movie may rate 5 stars but a slip on the keys and it’s a 4 in the compendium.

    So I’d like to change our “stars” shortcode from simply displaying graphical stars to instead pass it the URL for the review and have it use that to find the record in the database and pull the Rating from it. The URL for the review is in the sidebar and the compendium so it’s already part of the process.

    How would I go about doing that to get the data from the custom field?

  • Hi @Agrajag

    May I ask why you need to wrap all this within a shortcode?

    The ACF plugin is best used by editing your theme and using PHP code to render data.

    Perhaps you could provide some screenshots to show why the shortcode is needed?

    I don’t think WP has a function to find a post via a url (I may be wrong). So I would be more inclined to use a post_id instead.


  • Thanks for the reply Elliot.

    Have a look at this page:

    While on the page, note the sidebar on the right called, “Latest Review Roundup”. It’s a small subset of the full list above.

    If there was a simple way to get the post_ID in the editing phase I could easily use that.

    Later I plan to automate — fully — the creation of both of the above areas but, for now, I’m nowhere near that level of coding expertise to make that happen.

    My genesis for this idea was a plugin called Xavin’s Review Ratings. You use a shortcode to just make graphical stars appear wherever you want. For a site like ours, that’s useful. However, as the first page above grew in size we ran into an issue. Their shortcode created the images one star at a time so every line created five calls to draw those stars. The page took 30-50 seconds to render. I then created a much more efficient shortcode that links to images that are already pre-built with five stars (thus a 3-star review would call a single image that has 5 stars with 3 gold and two blank). The difference is the page now loads in a few seconds.

    However, I then compared notes with our Editor and we realized between us that we were finding mistakes between instances of the references to each movie. The entry for a movie is this:

    [stars rating=5 set=tiny] <a href="" title="by Rich Heimlich 10/25/13">12 Years a Slave</a>

    We just paste that into a text widget for the sidebar and the Page for the Compendium page.

    Is there a better way? I’m new to WP (switched from Blogger less than a year ago) and PHP coding so I won’t be insulted that I’m doing this all wrong. hehe

  • Hi @Agrajag

    The better way is to create this data dynamically. Are you manually editing this sidebar content each time a new movie comes out?

    The smart approach would be to code a PHP driven widget that loops through the latest x amount of movies and then (using a radio button field on each movie), use the movie’s rating to render out the amount of stars.

    I would create a custom field on each movie to select 1,2,3,4 or 5 as a value.

    Then you will need to re-code your sidebar to loop through the movies using a get_posts function.

    Since you are new to to WP and PHP, I doubt you will be comfortable coding this yourself. I would recommend that you contact a freelance to write this code for you as the ACF plugin unfortunately requires you to have solid WP and PHP skills.


  • Thanks Elliot. That’s the plan over time. I was hoping to come up with a mild work-around to help teach me this as a good learning project. The reviews already use ACF with a custom field that contains a numeric for every film (fractional as each film can have half-stars). That I had no problem with.

    We do edit the sidebar almost every day to add the entries manually.

    The complexity in a full widget app is “x amount of movies” is a moving target. Summer makes for a longer list. Some films hang around MUCH longer than others and need a way to be held over so it can’t simply be a case of showing 10 films in the list or showing from one date to another.

    As I said, I did manage to get the custom field working fine (that’s what’s feeding the actual review posts) and I managed to write my first Shortcode to display stars based on manual entries so I’m getting there.

    The wall I hit was in not having any clue how to go the next step and you’re suggesting it’s a BIG one. We may need to pay someone but right now that’s just not in the budget.

  • Hi @Agrajag

    Thanks for the follow up. Can we mark this topic as resolved, or do you have any other queries?


  • I’d still be interested to see a tiny code snippet showing how a call to a custom field (say, Rating, for example) would look. That’s it.

    Thanks again.

  • Hi @Agrajag

    Calling a custom field looks like so:

    $rating = get_field('rating', 123);

    Where 123 is the post_id you want to load the value from.

    Does that help?


Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘DB query for field via URL’ is closed to new replies.