Wordpress - How to get the most viewed posts? - mysql

How can I display my most viewed posts on a single page ?
For this moment, i've create a new template page (content-most-viewed.php) wich is called by get_template_part();
On Internet, i've read that query_posts could solve my problem, but it seems to fail.
This is my template :
<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
//query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC');?>
<div class="post">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="heading">
<h2><?php the_title();?></h2>
</div>
<?php the_content();?>
<div class="link-holder">Retour</div>
<?php endwhile; ?>
<?php else : ?>
<div class="heading">
<h2>Not Found</h2>
</div>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
But, I can only get recent posts, or "Not Found" (any resutls)
In advance, thanks !

WordPress by itself does not keep track of the number a post is viewed there are pluging that do this: WPP, plugin repos
These plugins will have build in functions to do this.
side note
don't use query_posts its bad for preformance. use WP_query in stead. reasons why it is bad: https://wordpress.stackexchange.com/a/50762/10911, https://wordpress.stackexchange.com/a/50762/10911

Related

Wordpress Polylang adding pll_e breaks HTML

i'm using polylang to translate my blog site along with loco translate. i'm manually adding string translations which was working fine with get_theme_mod parts, but there is a place that i want to also add custom string translation, after i add manually it breaks html and css won't work then.
it should be seen like this after adding custom string translation ; works fine without pll_e
but after i add pll_e to the that part in index.html ;
<?php get_header(); ?>
<div class="content">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<?php echo get_template_part(pll_e ('inc/page-title') ); ?>
<?php endif; ?>
it breaks the html but translation works. its seen like this ; looks like this
does anyone knows the solution ? i think its about get_template_part and get_theme_mod because the same things that i've done with get_theme_mod parts works fine.
by the way there is difference like this with pll_e and without it.
without pll_e
with pll_e
i solved the problem by editing index.html like this ;
<div class="content">
<div class="page-title group">
<div class="page-title-inner group">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<h2> <?php echo get_template_part(pll_e('inc/page-title') ); ?> </h2>
<?php endif; ?>
</div><!--/.page-title-inner-->

is_single() function dose not show thumbnail image

I am using get_template_part('content'); in single.php. The the_post_thumbnail('large-thumbnail'); function shows the thumbnail image perfectly before. But it does not show with is_single() function in content.php.
content.php code:
<?php
if(is_single()){
the_post_thumbnail('large-thumbnail');
the_content();
}else{
the_post_thumbnail('small-thumbnail');
echo get_the_excerpt();
?>
Read moreĀ»
<?php } ?>
single.php:
<article>
<div class="single_post_content">
<h5><?php the_title(); ?></h5>
<?php
get_template_part('content');
?>
</div>
</article>
I think you forget to write wp loop in your single.php file. follow this code,
<?php while ( have_posts() ) : the_post();
get_template_part('content');
endwhile; wp_reset_postdata(); ?>
the_post_thumbnail() do not work out of wp loop, So call your get_template_part() function between the loop. May be now its will solve your problem.

Wrong post title displaying from wordpress loop

I'm building my own theme. I have a page set for my blog (with a template that I made) which I'd like to just display a few of my posts. It uses the following loop:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_post();
the_content();
?>
</div>
<?php
}
}
?>
The titles of my posts are "First Post, Second Post, Third Post, and Fourth Post" respectively. When the posts are displayed on the blog page, they are displayed in the correct order, but the titles of the posts are incorrect. The first post's title reads: "Second Post". The second post's title is: "Third Post", and so on until the last (most recent) post which has the title: "Blog" (the page title). What happened to the titles that they got so screwed up?
What I've Tried:
I've researched this a lot before I came here. I tried using get_the_title() instead but that lead to no titles being displayed. I've also tried using the_title_attribute() to no avail. I also understand that I shouldn't be using query_posts for this loop but I'm unsure which is the correct method to use for getting the posts in this particular case. Most of the info I've read was unclear though and didn't seem to fix the issue.
Any help is greatly appreciated.
Try something like this: untested
<?php
global $post;
$args = array();
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
YOUR HTML HERE
<?php endforeach;
wp_reset_postdata(); ?>
Okay Nevermind! I just found out what the problem was. I moved, "the_post()" to just after the while loop so now it reads like this:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
the_post();
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_content();
?>
</div>
<?php
}
}
?>
I found this solution while I was reading about the_post() in the wordpress codex. Turns out this function is setting up information for the next post in the line, so it shouldn't be mixed in with the html output for the current post.
In regards to whether or not I should be using "query_posts()" is still something I'm unsure of and willing to take any advice. But the loop in it's current form is working.

Image Slider flashes wrong image at first

I am using a wordpress theme, Pitch Pro, and it has a built in slider. My issue is that sometimes the slider will flash the wrong image for a second. I noticed that it does not do it normally but only if I go from a page such as support to the home page. I have tried changed and setting the order. Changing the max amount of slides, currently 6, but nothing seems to fix it.
The site currently is jrummy16.com/test if anyone has any idea on a fix.
Not sure if this could relate to it anyway but the server I am using has issues and to fix it hostgator told me to add define( 'CONCATENATE_SCRIPTS', false ); to my config.php file. It fixed the issue but I do not recall the slider having this issue before adding that.
I have no idea on how to even start troubleshooting this. So any help would be greatly appreciated.
EDIT
All of the files were found inside the Theme, Pitch Pro.
I opened up my home.php file and I find this at the top for slider.
<?php
$slider_template = apply_filters('pitch_slider_template', array('slider', null));
get_template_part( $slider_template[0], $slider_template[1] );
?>
I then went and opened up slider.php in the same folder. It has this code,
<?php
$slides = new WP_Query(array(
'numberposts' => siteorigin_setting('slider_max_slides'),
'nopaging' => true,
'post_type' => 'slide',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($slides->have_posts()){
?>
<div id="slider">
<div class="container">
<div class="slides nivoSlider">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<?php if(get_post_meta(get_the_ID(), 'slide_destination', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination', true) ?>
<?php echo '<a href="'.esc_url(get_permalink($destination)).'" title="'.esc_attr(get_the_title($destination)).'">' ?>
<?php elseif(get_post_meta(get_the_ID(), 'slide_destination_url', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination_url', true) ?>
<?php echo '<a href="'.esc_url($destination).'">' ?>
<?php endif; ?>
<?php echo get_the_post_thumbnail(get_the_ID(), 'slide') ?>
<?php if(!empty($destination)) echo '</a>' ?>
<?php endif; endwhile; ?>
</div>
<?php $slides->rewind_posts(); ?>
<div class="indicators-wrapper">
<ul class="indicators">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<li class="indicator <?php if($slides->current_post == 0) echo 'active' ?> indicator-group-<?php echo $slides->post_count ?>">
<div class="indicator-container">
<div class="pointer"></div>
<h4><?php the_title() ?></h4>
<?php the_excerpt() ?>
</div>
</li>
<?php endif; endwhile; ?>
</ul>
</div>
</div>
</div>
<?php
wp_reset_postdata();
}
I am not sure if this will help or not but I hope that it does.
I think the reason is that before slider loads completely, the page will show last image in the list of slides and in your case it is this one:
<img width="705" height="344" src="http://jrummy16.com/test/wp-content/uploads/BA-slider.jpg" class="attachment-slide wp-post-image" alt="BA-slider">
Here is a similar problem and it has few of the solutions which you may use it as well.
UPDATE:
I found this article which suggests to modify your CSS. I would paste the code here but the article uses image to show CSS code:)

When Logged out, one blog shows - logged in, all blogs show

I've coded this Wordpress site but I'm finding difficulties with the blog area. Only one blog shows in the blog section but with I am logged in as an admin, I can see all the blogs on the page. I need it to show for everyone, whether they are logged in as a user or just a visitor to the website.
I have attempted to see if it was a wordpress issue by checking the 'Settings >> Reading' settings and they are set just fine, showing to be 10 posts per page.. It could be something wrong with the loop. I have the blog pulling from the index.php.
http://www.ilovepennycakes.com/category/blog/
Here is the direct link to the blog not showing in the feed.
http://www.ilovepennycakes.com/thanksgiving-thoughts/
The code is as follows:
<?php get_header(); ?>
<!-- Article Loop -->
<article>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="news-top"></div>
<div class="news">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-header">
<h1 class="meander"><?php the_title(); ?></h1>
<p class="likes m500"><?php comments_popup_link( '0', '1', '%' ); ?></p>
<div class="clear"></div>
</div><!--end post header-->
<!--div class="entry clear"-->
<div class="blog-content m500">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
</div>
<!--/div--><!--end entry-->
<p class="date M500">Posted <?php the_time( 'j M Y' ); ?></p>
<p class="M500"><?php edit_post_link( __( 'Edit', 'pennycakes' ), '<span>', '</span>' ); ?></p>
<!--end post footer-->
</div><!--end post-->
</div>
<div class="news-bottom"></div>
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
</div><!--end navigation-->
<div class="navigation index">
<div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
<div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
<?php else : ?>
<?php endif; ?>
</article>
<!-- //Article Loop -->
Any help will be appreciated.
Hmmm... are your posts still "Drafts" or published? If they are drafts, it is normal to be able to see them only if you are logged in, because a draft is not published so not supposed to be seen by any visitor. That's my guess. Otherwise, please give more info.
Ok, so I have the answer for you: index.php is not the file used to display http://www.ilovepennycakes.com/category/blog/. The file used for this type of archive display is "category.php". If there is no such file in your theme folder, add it (possibly duplicate index.php as you have programmed it or take it from a default example template).