Hello @nathanssimpson,
Haha! I know how you feel, it can take a long time to get these things working. I'll try to help you get it working. Here are some instructions:
Notes:
- I am using the WP-Clear theme (3.2)
- I wanted to order all category posts by the number of Facebook likes I have the following two plugins: Ultimate Facebook (premium plugin http://premium.wpmudev.org/project/ultimate-facebook/). I'm very sure that you can do it without this to be honest, but you'll need some way of getting all the Facebook stuff working. If you look on the Facebook developers site and use the set-up there it should work. Or, if you get a free Facebook plugin just so that the shortcodes for it to work that should be enough. And WP User Frontend (http://wordpress.org/extend/plugins/wp-user-frontend/)
- On my website I allow logged in users (they can only log in with Facebook) to add posts - this is what Wp user front end does.
Actions
Get above two plugins installed and working. In order to get the Facebook likes part working I added a custom field to every new post. Because I didn't want to have to do it manually (because people can post any time of day or night), I modified the WP User Frontend plugin to add a new custom field when a new post was added. To do that, edit the wpuf-add-post.php file from within the plugin's directory and add a new line just below the if ($post_id) { line, as below:
if ( $post_id ) {
//add mandatory FB likes custom field. Added by RJ
add_post_meta( $post_id, 'fb_likes', '0', true );
That will add a new custom field called fb_likes every time a post is added but ONLY through the Wp User Frontend plugin. If you add new posts manually, you should probably add it as well.
Next, add the code for a like button to your post (single.php). I think you can probably just use the Ultimate Facebook plugin but I haven't tested that yet. Here is the Facebook button code and the code to capture when you get a click on the like button:
Put this at the top of single.php
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#" >
<div id="fb-root"></div>
Then you need to find a way of getting the post id. I used this:
$vpostid = $post->ID; ?>
Next, add the Ajax code, the event capture and the FB like button code:
<script>
FB.Event.subscribe('edge.create',
function(response) {
jQuery.post("http://www.my-domain.com/fbtest.php", { category: <?php echo $vpostid; ?> } );
}
);
</script>
<fb:like send="true" width="450" show_faces="true" font="verdana"></fb:like>
You'll notice in the above that there is a fbtest.php file. You will need to create that, too. Here is the code I used:
<?php
require( './wp-load.php' );
global $wpdb;
$vcatid = $_POST['category'];
$meta_val = get_post_meta($vcatid, 'fb_likes', true);
$fbmetval = intval($meta_val);
$fbmetval = $fbmetval + 1;
update_post_meta($vcatid, 'fb_likes', $fbmetval);
die();
?>
Finally, and this works if you want the posts to be ordered by the fb_likes custom field on the category sub-pages (not sure of the technical term for this...), add this code to index1.php:
// Added by RJ for ordering by FB links custom field
if (is_category()) {
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'meta_key' => 'fb_likes', 'orderby' => 'meta_value' ) );
query_posts( $args );
}
You should add the above code just below this piece of existing code in index1.php:
if (is_home() || is_page()) {
query_posts(array(
'post__not_in' => $do_not_duplicate,
'paged' => $paged
));
}
Bear in mind that some of this might be specific to my theme. I am use WP-Clear by Solostream. I've not looked at any other themes before so I don;t know if single.php, etc are common names and layouts for the files.
Let me know how you get on. Good luck!
Rob