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

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.

Related

Order archive posts by number of social shares

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?

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.

WP_Query with WPML returns different results on custom post types pages

I am using Wordpress with WPML plugin (2 languages). I execute following WP_Query in header of all pages to get the results of my custom post type (slideshow):
$slideshow = new WP_Query(array( 'post_type' => 'slideshow', 'showposts' => 20 ));
Query works fine on homepage, posts, pages, archives and returns last 20 items from my custom post type "slideshow" in CURRENT language. But when I visit my custom post type page (e.g. /custom-post-type/post-name) the same query returns last 20 items from ALL languages which is wrong!
What can I do? I also tried experimenting with supress_filters true/false as suggested on various forums around the web but with no luck - it returns the same posts in any case.
The problem was in custom post slugs translation...
To fix this navigate to WPML > Translation management > Multilingual content setup and UNCHECK "Translate custom posts slugs (via WPML String Translation)"

Change wordpress blog limit to months rather than by number of posts

Is there a way to override how wordpress displays a number of posts on a listing of posts page, regardless of page type (archive, category, posts, tags etc.)?
I'm transferring content from a static site where the pages are saved using a month format.
I'd like to be able to set wordpress' previous/next link navigation to work using months, rather than the limit of posts per page.
So if the navigation would go forward/backward to the previous/next month where there is published content.
Thanks
Perhaps a WordPress Query will work?
Such as... (Not tested)
$month = date('m');
$query = new WP_Query( 'monthnum=' . $month );
As the WordPress Codex lists

How to display non featured wordpress posts

I have upload a plugin as 'featured posts' to add featured posts, also adding regular posts, both type of posts having default category(say, buy know). I want to display only regular posts(i.e non featured), but I unable to display only the regular posts, its displaying total posts(i.e regular plus featured). Please any one help me to display only the regular posts.
Thanks
Shama
$feature_ids=array();
$query=query_posts('featured'=>'yes');
while(have_posts()):the_post();
$feature_ids[]=get_the_ID();
endwhile;wp_reset_query();
// now your feature posts id store in $feature_ids as array then
// in actual query
$query=new WP_Query('post__not_in'=>$feature_ids);
// it will exclude the posts that are feature
if(have_posts()):while(have_posts()):the_post();
// yours data
endwhile;endif;wp_reset_query();

Resources