orderby comment_count not working - sidebar

I'm trying to display the 5 most commented posts of the past week in a Wordpress sidebar. Simplified I'm using this code:
<ul class="popular_posts">
<?php
wp_reset_postdata();
$pc = new WP_Query('orderby=comment_count&order=DESC&posts_per_page=5');
while ($pc->have_posts()) : $pc->the_post(); ?>
<li>
<strong><?php comments_popup_link('', '1', '%'); ?></strong>
</li>
<?php endwhile; ?>
</ul>
It works on every page apart from the home page where it just shows them in a random order. It's as if it ignores the orderby part.
I've tried:
putting wp_reset_postdata() before/after the query
using posts_orderby instead of orderby
preceding the code with remove_all_filters('posts_orderby')
Has anyone got any idea what I'm missing? I've spent the past 2 day trying to figure this out with no joy.
Thanks!

The following code is a solution for your issue!
$most_comment_posts_args = array(
'orderby' => 'comment_count',
'posts_per_page' => 9);
$most_comment_posts= new WP_Query($most_comment_posts_args );

Related

Remove div class post nocolsidepost - wordpress

My wordpress theme (Anglepane) is creating a second (unwanted) post at the bottom of my page.
I have used Firefox page inspector and isolated the problem with the div tag "nocolsidepost", and I have gone through the 20+ theme files (css etc) and deleted this tag whereever I find it, but the second posts still remain!! - I can delete it on the fly with Firefox page inspector, but not in the actual code
Could anyone please explain how to solve this
Website is http://historyofliverpool.com and the section I want to remove it the bottom block with the previous / next links on it
I don't know the structure of the code in the theme but it can be in multiple parts of the code.
TwentyThirteen and TwentyFourteen themes (the ones bundled with Wordpress) use a separate function to show the pagination or the Prev/Next buttons. If your theme has this function (usually in function.php file) you will have to search for it and delete/modify all the lines that print something. For example, here is the function from TwentyThirteen theme:
function twentythirteen_paging_nav() {
global $wp_query;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 )
return;
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentythirteen' ); ?></h1>
<div class="nav-links">
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentythirteen' ) ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentythirteen' ) ); ?></div>
<?php endif; ?>
</div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}
On the other side, the theme can easily implement the corresponding code directly in the archive.php, index.php and other category.php/tag.php files. It is similar to the function used above, the only difference is the function wrapper (the first is a function, the second not).
Or, simpler (but less proper) way to achieve this is to add a display:none; to the element above. But, to achieve this, you will have to find the code that generates it, to add there an id, and in the CSS use the id like this:
#your-id-here{
display:none;
}

If statement not working when it really should

Okay, this is by far one of the strangest things I've come across in some time.. I have the following piece of code in one of my woocommerce files:
<?php $titlevar = wp_title('', 0); ?>
<?php echo $titlevar; ?>
<?php if($titlevar == "Men") { ?>
<div id="sidemenu" style="float:left;width: 180px;float:left;height:100px">
<?php wp_nav_menu( array('menu' => 'sidemenumen' )); ?>
</div>
<?php } ?>
As you can see, if my $titlevar variable equals "Men", it should display a div with a menu inside. I've echo'ed the variable at the top and it actually outputs 'Men' (without quotes of course), how is it possible that it's not showing my div??

How to loop through the custom post type once you have added to an existing query

I am a bit stuck with a project and I would appreciate if someone could help me with that one.
I've searched for some references everywhere (firstly of course here) and I only could find bits and pieces of what I need.
I need to end up with the following structure:
<!--LOOP 1 - WORD PRESS PARENT PAGES-->
<div id="post-parent1-name">...</div>
<div id="post-parent2-name">
<h2><?php the_title(); ?></h2>
<div class='post-content'><?php the_content(); ?></div>
<!--LOOP 2 - WORD PRESS CHILDREN PAGES-->
<div id="post-child1-name">...</div>
<div id="post-child2-name">
<h3><?php the_title(); ?></h3>
<!--LOOP 3 - WORD PRESS CUSTOM POST TYPE-->
<div id="custom-post-type-name">
<h4><?php the_title(); ?></h4>
<div class='post-content'><?php the_content(); ?></div>
</div>
</div>
<div id="post-child3-name">...</div>
</div>
<div id="post-parent3-name">...</div>
<div id="post-parent4-name">...</div>
I've used the following filter in my functions.php to add the custom post type to the query:
<?php
function insert_post( $query ) {
if ( $post->post_name == 'my_parent_post_name' && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'my_custom_post_type') );
return $query;
}
add_filter( 'pre_get_posts', 'insert_post' );
?>
I've managed to get the first and the second one to work fine however I could not figure out how to get the third one to work (which is my custom post type) in order to bring me the title, content and thumbnail respectively.
Sorry guys, I know that looks a bit confusing however I hope that makes sense.
Cheers,
Doug.
After several hours trying to find the answer for that problem I finally achieved my goal using:
<?php global $wp_query; ?>
Function Reference/query posts
I am still not quite sure about the usage of the action add_action( 'pre_get_posts' , 'ucc_pre_get_posts_filter' );, although I've manage to add a new array to my pre-existing query without affecting it:
Pre-existing query:
<?php query_posts(array('post_type' => 'page', 'order'=>'ASC', 'post_parent' => 0)); ?>
First Loop here
Inclusion of a new array using $wp_query:
<?php
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => 5 ) );
query_posts( $args );
?>
Third Loop here
Cheers guys!

Different sidebars for different pages on Wordpress

I'm trying to design a theme on WordPress (version 3.3.2) but I am having a few problems in having the sidebar display a different set of widgets on certain pages.
I have tried several online tutorials and this one in particular http://www.rvoodoo.com/projects/wordpress/wordpress-tip-different-sidebars-on-different-pages/ but unfortunately there is no change in the sidebar when I move to a different page
I registered two sidebars on my functions.php as shown below, one which I would consider as main, and the other as a custom sidebar, and I also added different widgets to these sidebars.
<?php register_sidebar( //register sidebar
array(
'name' => 'Right-side',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar( //register second sidebar
array(
'name' => 'Second-right',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
?>
Following that, I created the files sidebar.php and sidebar-second.php to be able to call them.
sidebar.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Right-side')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
sidebar-second.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Second-right')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
And then I added replace my <?php get_sidebar() ; ?> statement with following conditional
<?php if( is_page('225') ) : ?>
<?php dynamic_sidebar('second'); ?>
<?php else : ?>
<?php get_sidebar() ; ?>
<?php endif ; ?>
However only the widgets added to the sidebar.php are displayed on every page. Any help on how I could change this, or pointers on what I could be doing wrong. Thanks.
I looks like you are using <?php dynamic_sidebar('second'); ?> where you should be using <?php get-sidebar('second'); ?>
What get_sidebar('foo') does is look for a file named 'sidebar-foo.php' in the root directory of your theme and includes it. dynamic_sidebar('bar'), on the other hand, looks for a sidebar registered with:
<?php register_sidebar( array( 'name' => 'bar' ... ) ) ?>
Hope this helps!
Why don't you use the Custom Sidebar Plugin?
It's really easy to use and does not need any coding
You have two sidebars. when you want to use the sidebar-second file on a particular page, instead of calling
<?php get_sidebar('second') ; ?>
TRY
<?php get_sidebar('sidebar-second.php') ; ?>
this should do the trick

how to fetch next row when given row is hidden using pagination?

It rather hard for me to come up with a short form of my question so bear with me:
Using jQuery and AJAX, I have all posts stored in my MySQL database displaying on index.php. Each post has a button the lets the user hide that post's DIV.
Now, I want to make it so that I only see 10 results and paginate the rest. I know there are plenty of pagination plugins out there but none of them displays the next post when the user hides one of the 10 already shown posts.
So if I see Posts 1-10 on page 1 (posts 11-20 on page 2) and user hides post 8, I should see Posts 1,2,3,4,5,6,7,9,10,11 on page 1 with posts 12-21 on page 2 and so on.
Question is: How do I do that? Here's what I have right now (fetch posts from database, display posts on page + hide button toggling):
// Javascript
$(document).ready(function() {
$(document).on("click", ".hide", function(){
postID = $(this).attr('id').replace('hide_', '');
$('#post-' + postID).toggle();
return false;
});
});
// PHP
<?php
$pageposts = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts"), OBJECT);
?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div id="buttons_<?php the_ID(); ?>">
<div id="hide_<?php the_ID(); ?>" class="hide" style="position:absolute; right: 2.5em;">
<a id="hidebtn_<?php the_ID(); ?>" href="#"><span></span></a>
</div>
</div>
<div id="post-<?php the_ID(); ?>">
/* display post content */
</div>
<?php endforeach; ?>
<?php endif; ?>
An easy way to figure out what would be the next ID to grab would be to store the MAX id on the page, so if your page displays 1-10, 10 would be stored as the MAX id.
When a user hides a post, the mysql query would pull 1 result where id > the stored max id.