Order archive posts by number of social shares - wordpress

I have Yoast Seo plugin installed into my website and this brings back the number of social shares per post and stores it in the database.
Is it possible to change the archive page to order the posts by the number of social shares?
I've tried a few things listed here but can't seem to get it to work
<?php
/* The loop */
while ( have_posts() ) : the_post();
include( locate_template( 'content-' . $layout_this . '.php' ) );
endwhile;
?>

This one might be tougher than you think. Yoast SEO does not actually store the number of times any given social media outlet is actually shared. This is done by constantly running a check from the respective social media API and will retrieve the number of shares/likes which happened on that respective post.
Essentially what needs to be done is you'd need to hook onto the API a similar way Yoast is doing so, and then start comparing the values which are being sent back from the API in order to order your posts on the front-end by a number of most shares.
The idea for the functionality is pretty useful... I'm hoping that in the future there will be a new table added perhaps with the Yoast plugin which will actually save this data rather than having to refer to the API.
EDIT: In theory, this should be possible then if you know what meta value(s) you're working with from the database. As long as the posts have association with these values, this should be possible. You can query posts with specific meta keys and order them, too. Here's an example of what might work within your template, with some adjustments.
$args= query_posts(
array( 'post_type' => 'your post type', //for posts, enter 'post'
'order' => 'ASC',
'meta_key' => 'some_key', //the meta key
'orderby' => 'meta_value', //or 'meta_value_num'
)
);
See here as well, someone asked a very similar question : How to fetch posts order by meta value?

Related

Singular/Plural URL Structure in WordPress for Post Types

I am working on a client's website, and he has asked me a very genuine question, and I am unable to figure it out.
He is Creating a Website for Piano Chords, where he will write tutorials about chords in general and each individual chords too. And he wants them to be of a similar post type.
So I registered a CPT Chords with the slug chords using the documentation on Codex.
Now the problem is the site has multiple posts about chords. And they will be of 2 kinds.
One will be generic to all chords, hence the word needed in the slug is chords and not the singular form.
e.g. -> https://mypianonotes.com/chords/overview/
This is fine. Now problem is when we need content like "C Major Chord". This is supposed to be singular. So the slug/url should be chord/c-major/ and not chords/c-major/.
Is there any solution to this problem? Or will I have to create 2 separate post types?
Register the post type as 'my_chords' then add a rewrite rule parameter in the arguments like this
<?php
$args = array(
'rewrite' => array('with_front'=>false, 'slug'=>'chord'),
);
register_post_type( 'my_chords', $args );
?>
Then create a page for 'chords' with a child page of 'overview'.
Hope this helps :)

Wordpress thinks custom taxonomy page is search page

I have set up a custom taxonomy for regular posts called guide.
I have a template file called taxonomy-guide.php which lists out posts with a guide taxonomy.
So my URL structure appears like this: http://www.example.com/guide/new-york-city.
This all works fine and dandy, the pages are listing out the posts categorized with the correct guide.
However, this is the issue, the page title on those URLs is You searched for | My Site Name, which is what it should be only on search pages.
Relevant, I'm using the Yoast SEO Plugin, and it even has an area to edit the Titles and Meta for your taxonomies, which I have, and the changes there are not taking effect, no matter what I put there it's still showing You searched for | My Site Name.
Do you think there is some sort of URL structure conflict, or what could be causing this issue?
Update
I have disabled the Yoast SEO Plugin and now the titles on those pages are | Search Results | My Site Name, so it still thinks it's a search results page.
2nd Update
I have this at the top of my taxonomy-guide.php file in order to update the sort order:
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'date', 'order' => 'ASC', 'parent' => 0 ) );
query_posts( $args );
If I remove this, the page title is fixed, although now the posts aren't in the order I want. Any idea why this would be causing the page title to change? Or how I can change the order a different way to see if that fixes it?
Solution
If I change the above code in the 2nd Update to
query_posts($query_string . '&orderby=date&order=ASC&parent=0');
It works as expected. Not sure what was causing the page title to change in the code in my 2nd Update, but that's an odd one.

Custom WP_Query not working (listing featured articles + popular articles)

I am Wordpress beginer and I want to set multiple queries on a page that lists my posts.
Featured posts
I have front page that lists featured posts from all categories,
Categories pages that lists featured posts from current category
Q: Is it better to use My featured posts new category name or to set sticky post (under public - publish options) for posts I want to feature on front page and on categories pages? Every post already have his own category like News so the My featured posts will be the second category.
Query
Let's suppose I'm using category name for featured posts (but then i got permalink to My featured posts category (site/my-featured-posts/2013/07.....) which I don't want (so maybe sticky posts are better solution)
I'm trying to setup query to list featured posts but only from standard post type, not gallery or video
<?php
$arg = array(
'cattegory_name' => 'my-featured-category',
'posts_per_page' => 5,
'nopaging' => true,
'post_status' => 'publish',
'post_type' => 'post'
);
$featured= new WP_Query($arg);
if ($featured->have_posts()):
while ($featured->have_posts()) :
$featured->the_post();
?>
and then below the_title(); ..... and so on
What I get is all articles from all categories.
Q: Also, how to I get popular articles based on views and number of comments for the last day?
Q: How to list posts which have post format video?
Q: Are there any online tools that build wp_query based on criteria?
Thank you.
You can also set posts as "featured" using a tag. You can then use tag=featured with WP_Query to get posts tagged with a certain tag ("featured" in this example).
As for your additional questions...
Q1: You can save page views to a flat file or to the WordPress database. Once I used the database to store a UNIX-timestamp as traditional WP post meta for each post page view. As it was a timestamp, I could calculate the age of the page view easily (and I could also run SQL queries to remove post meta that was older than a certain timespan).
Comments can be queried by time ordering too. See WP_Comment_Query.
Of course there are plugins that can do this for you, but for me they included useless bloat I did not need.
Q2: For querying post formats, use the query variable called tax_query as GATEKeeper did in a WP support question: http://wordpress.org/support/topic/post-formats#post-2034414.
Q3: A google query returned a thing called The WordPress Query Generator.

How to speed up IMDb ratings plugin in WP?

I tried to implement IMDb ratings plugin in WP, in this way:
I made a meta box and I copy in it the HTML code generated by the IMDb ratings plugin from IMDb site, here is the code generated by plugin, but when loading the website, plugin content is very slow (a few seconds).
Meta box:
array(
'label' => 'iMDb Rating',
'desc' => 'iMDb Rating Script',
'id' => 'imdb-rating',
'priority' => 'high',
'type' => 'textarea',
'std' => ''
),
Display meta box method in post:
<?php $test_field_value = get_post_meta( get_the_ID(), 'imdb-rating', true ); echo $test_field_value; ?>
Question:
Is there a way to make the plugin to load faster, or to implement it in a good way to avoid the slow display?
I asked on wordpress.stackexchange too, but i was redirected here.
2 ways I can suggest:
Load the IMDB data using AJAX, that way page will render first and then you can load the rest of the data. You could even create a custom wordpress page in your theme that loads the plugin and call this from your on page js.
Cache the data so that you only reload on a regular basis. Ive not used the plugin so my guess is you would probably need to do a little rewriting to get that happening.

Wordpress Sticky Posts with Custom Post Types

So i need the ability to have a featured or "sticky" post in wordpress, and it occurred to me! Why not use the Sticky Posts facility, but after doing a bit of reading it seems that Wordpress decided to not include support for it in latest releases and they don't seem to be pushing any solution for future releases.
Now that leaves me in a predicament i wish to have the ability to have a featured post or custom post without using a category of such.
I've also seen a few people state they have hacked wordpress with possibly a function to add the ability of sticky posts to custom post types, shame they didn't share the source!
How would this be done?
You can do it with a custom field (post_meta) on the custom post type. Then fire a custom query that selects for the meta_value:
$args = array('post_type' => 'my_custom_post_type', 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => 'is_sticky', 'value' => '1', 'compare' => '=', 'type' => 'CHAR')));
$sticky_posts = new WP_Query($args);
Should return an array of published posts of post_type: my_custom_post_type that have the sticky flag set.
Although I haven't tested the above code, I'm doing something similar and it works fine.
You can use this plugin, it has it's own limitations, but works pretty well if you don't need something elaborate.
You can save a custom meta with the name of "sticky" and add it the value "on" when the post is sticky. That can be done with a custom metabox and a checkbox.
Wordpress will automatically add the word "Sticky" on the backend posts listing table
You can retrieve a loop with your sticky custom posts by adding the values 'meta_key' => 'sticky' and 'meta_value' => 'on' to the args of your query
I posted a working solution as of WordPress 4.2 here:
https://wordpress.stackexchange.com/questions/90958/adding-sticky-functionality-to-custom-post-type-archives/185915#185915
Basically, it implies installing a small plugin and add a code snippet.
I have Wordpress 3.2.1, the latest version and I can sticky posts. It works for me on my site.

Resources