• Resolved ZaddKeefer

    (@zaddkeefer)


    Hey guys!

    My problem is I want to order posts with the meta_key “rating”, but the problem is the meta_value is a direct link to an IMDB movie (url) fetching the imdb rating and transform it into the imdb rating on output Is there a way around this, so it can sort by the transformed output and not the meta_value field that contains an URL?

    I use the plugin “imdb-connector” to help fetch and transform the rating, is there a way to use imdb-connector PHP functions to order the output from the meta_value field?

    My code:

    <?php
    if ($_POST['select'] == 'prijsn') { $order = "&order=DESC&meta_key=rating&orderby=meta_value";
    ?>

    Is there a way to implement the meta_value “rating” of meta-key to be the output on the site? with use of this code in some kind:

    <?php
    $rating="Rating";
    $imdbrating = get_post_meta($post->ID, $rating, true);
    $movie = imdb_connector_get_movie($imdbrating);
    echo $movie["imdbrating"];
    ?>

    https://wordpress.org/plugins/imdb-connector/

Viewing 15 replies - 1 through 15 (of 28 total)
  • Thread Starter ZaddKeefer

    (@zaddkeefer)

    bump

    Plugin Author thaikolja

    (@thaikolja)

    Just to make sure I get you correctly: You want to list posts and order them based on their rating? If yes, how did you get the IMDb rating into the meta fields of the posts?

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Yes that’s correct? And ehm.. I got the imdb rating by using your plugin functions combined with an other function of WordPress by fetching the get_post_meta field I created under a post called “rating”, there I pasted the imdb link to the movie, marked in bold: http://www.imdb.com/title/tt1663202/ and used your function to fetch the rating, and transform it into the imdb rating:

    <?php imdb_connector_get_movie("Apocalypse Now"); ?>
    instead of typing in the title, which is kinda random since most movies/series are named the same.. So I made an argument called $imdbrating equal to WP get_post_meta function <?php get_post_meta($post->ID, $rating, true); ?> which was the url title that gave me an direct link and correct link to the movie I wanted in my meta field.. But that’s not the problem here. I want to know if there’s some sort of way to order by the output of my tranformed meta_field that is a rating such as “8.8” and not a title nor a part of an url.. The output works fine.. but wordpress seems to order by the field on my post backend and not on the output on my website.

    I want a workaround on this, thanks a bunch!

    Plugin Author thaikolja

    (@thaikolja)

    Please add me on Skype so we can discuss this in detail; I still have a few questions. When this is done, I’m going to post the solution here in this thread in case other users have a similar problem. My Skype ID is kewter1, I’ll be around in the afternoon on Saturday (German time).

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Would be cool to add you on skype and all that, but I don’t got skype on my current business laptop and with no mic nor cam.. so might be a problem currently -.- :/ Hopefully we can discuss this on the thread whenever tomorrow (danish time) here.. So hopefully I/you see a solution for this tomorrow mate!

    Thanks for responding fast and connecting on this thaikolja!

    I am always open to respond on chat on steam for further details.. real_player123 is my username

    Plugin Author thaikolja

    (@thaikolja)

    No problem, I’m confident we’re going to solve the issue. Talk to you tomorrow then.

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Sounds great, talk to you tomorrow mate

    Plugin Author thaikolja

    (@thaikolja)

    Can you give me your code? If possible, the whole theme so I can reproduce it.

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Sure thing.. Here is the code: http://codepad.org/jUndoldj

    The problem relies on the top of my code, the sorting functionality:

    <?php
    	$order = "&order=DESC";
    	if ($_POST['select'] == 'newest') { $order = "&order=DESC"; }
    	if ($_POST['select'] == 'prijs') { $order = "&order=DESC&meta_key=year&orderby=meta_value";  }
    	if ($_POST['select'] == 'prijsn') { $order = "&order=DESC<strong>&meta_key=rating&orderby=meta_value</strong>";  }
    ?>
    
    <form method="post" id="order" class="pull-right">
      <select name="select" class="btn dropdown-toggle" style="padding: 7px; background:#428bca; color: #fff;" type="button" onchange='this.form.submit()'>
            <option value="newest"<?php selected( $_POST['select'],'newest', 1 ); ?>>Recent Added</option>
            <option value="prijs"<?php selected( $_POST['select'],'prijs' , 1 ); ?>>Year</option>
            <option value="prijsn"<?php selected( $_POST['select'],'prijsn' , 1 ); ?>>Rating</option>
    </select>
    </form>
    <?php query_posts($query_string . $order); ?>

    I highlighted the thing I mean, it sorts well.. But the problem is, it does NOT sort by the rating on the frontend.. Which would be something like “8.8”, but sorts with this instead: , which isn’t “sortable” I want it to sort the outcome of the field

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Sure thing.. Here is the code: http://codepad.org/jUndoldj

    The problem relies on the top of my code, the sorting functionality:

    <?php
    	$order = "&order=DESC";
    	if ($_POST['select'] == 'newest') { $order = "&order=DESC"; }
    	if ($_POST['select'] == 'prijs') { $order = "&order=DESC&meta_key=year&orderby=meta_value";  }
    	if ($_POST['select'] == 'prijsn') { $order = "&order=DESC<strong>&meta_key=rating&orderby=meta_value</strong>";  }
    ?>

    I highlighted the thing I mean, it sorts well.. But the problem is, it does NOT sort by the rating on the frontend.. Which would be something like “8.8”, but sorts with the not transformed instead: https://gyazo.com/a639ff8ca88b905aade8c6298256b566

    Plugin Author thaikolja

    (@thaikolja)

    Instead of using orderby=meta_value, try using orderby=meta_value_num.

    Plugin Author thaikolja

    (@thaikolja)

    Hang on: Why do you even save the movie ID in the “Rating” post meta and not the actual rating?

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    Tried with the meta_value_num, didnt work out

    I use the movie id, so it fetch the dynamic/live rating.. I could use an easy way around this by just pasting the current rating.. But then its a static rating and not the live from imdb

    Plugin Author thaikolja

    (@thaikolja)

    Alright, if I’ve understood you correctly (which I’m not 100% certain of, lol), I might have a solution for you.

    I’ve built a class that can be used like this (here’s the source code of it):

    <?php
             $class = new IMDb_Movies();
             $class->set_update_interval(30, "seconds");
             $movies = $class->get_movies_by_rating();
    
             if($movies->have_posts()) {
                ?>
                <ol>
                   <?php
                      while($movies->have_posts()) {
                         $movies->the_post();
                         ?>
                         <li>
                            <strong><?php the_title(); ?>:</strong>
                            <?php echo get_post_meta(get_the_ID(), "imdb_rating", true); ?>/10
                         </li>
                         <?php
                      }
                   ?>
                </ol>
                <?php
             }
          ?>

    Explained: First, we define $class with the actual class that does the job. The line $class->set_update_interval(30, "seconds"); defines the interval the ratings of every post is being updated. In this case it’s 30 seconds which of course is a bit too quick but good for testing. That means every 30 seconds the rating is being fetched from IMDb.com again. You can choose “seconds”, “minutes” and “hours”.

    $movies = $class->get_movies_by_rating(); will give us a WP_Query object containing the posts ordered by their rating on IMDb.com. It accepts the $order parameter, so if you call $class->get_movies_by_rating("asc"), it will return them in an ascending order.

    A working file can be found here. It of course requires IMDb Connector to be enabled. The output should like this.

    I hope this solves your issue.

    Thread Starter ZaddKeefer

    (@zaddkeefer)

    I’ll try this out, and will come back with a reply, thanks for putting time into this!
    Will get back atchu, if anything of your code I don’t understand

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘Get the frontend/output of meta_value’ is closed to new replies.