WordPress : Issue with wp_query and gd star rating plugin - wordpress

I have used GD Star Rating plugin with my Wordpress installation, for creating 2 different sets of Ratings (Posts and Videos). (I was not able to find any other plugin that supports this feature).
I have used following arguments in one wp_query to display a list of top rated videos, and it's working fine.
$args=array(
'post_type' => 'youtube_videos',
'post_status' => 'publish',
'posts_per_page' => 10,
'gdsr_sort' => 'rating',
'gdsr_multi' => 3
);
On the same page, i am trying to show a list of top rated posts, with following arguments in wp_query, but it's not working.
$args=array(
'posts_per_page' => 4,
'post_type' => 'post',
'gdsr_sort' => 'rating',
'gdsr_multi' => 0
);
I found that issue is with value passes in 'gdsr_multi' varibale, if i remove this variable from the top rated videos 2nd part of code works fine.
I am not being able to run both together, any suggestions please?
For gdsr_multi variable documentation of GD Star rating plugin says:
For standard rating data, don't use this variable, or set it to zero.
To get multi ratings data this variable must be set to multi set id to use.

You should reset your query before start a new one. wp_reset_query(); will destroys the previous query used on a custom Loop. This function should be called after The Loop to ensure conditional tags work as expected. As example:
<?php
query_posts( 'post_parent=5' );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?><?php the_title() ?><br /><?php
endwhile;
endif;
wp_reset_query();
//your second query will be here.
?>
For more details, you can check Wordpress official codex: wp reset query

Related

Filtering Products of same price in one page wordpress

So i have an online store with variations of products but ill like to have a page that shows products with a price range. Say I have a page called 3k Store. I want such that when a user goes to that page, it filters and displays only goods that cost 3000, Please how do i go about this? I am using wordpress and woocommerce. Thanks.
1. Create a page template to list products
Create a page template to be able to put some code on it. Name it page.list-product-in-range.php for example and add this code at the start of the file to make it a template :
<?php // Template Name: List product in range ?>
<?php get_header(); ?>
<?php get_footer(); ?>
2. Create a page in the wordpress admin
Create a page named Product that cost 3000for example and select the template previously created. That's all for the moment.
3. Go to the php file that we created in part 1
Between <?php get_header(); ?> and <?php get_footer(); ?>, add this part to get the products that cost 3000 :
( I let the range in the meta_query so you can easily set something else than 3000 by playing with the number inside value)
<?php
$products = get_posts(
array(
'posts_per_page' => -1,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => array(
3000 ,
3000
),
'compare' => 'BETWEEN',
'type'=> 'NUMERIC'
)
)
)
);
?>
4. Add the loop to list products
After the code that we added in part 3 put this code :
<ul class="products">
<?php
if (!empty($products)) {
foreach ( $products as $post ) : setup_postdata($post);
wc_get_template_part( 'content', 'product' );
endforeach;
} else {
echo __( 'No products found', 'woocommerce' );
}
wp_reset_postdata();
?>
</ul>
You also can use pre_get_posts hook to set the products in the main query or instead of get_posts() use the WP_Query class. I only use get_posts() for the simplicity .
If you need pagination (and i think you will need), you should think about using pre_get_posts or WP_Query.
Code tested and it works.

WordPress wp_query + filter result (no custom filed) + next_post_link()

I did an extensive search but could not find the answer (perhaps I'm not using the right search terms?).
Anyway, here's what I'm trying to accomplish:
I'm using wp_query to return user submitted posts that have been published. I am showing one post at a time and using next_posts_link() to let the user to advance to the next post. This is all working fine. I'm also using the wp-postratings plugin to the user to rate these user submissions. I only want to show posts that the user has not already rated. For that, I'm using:
check_rated_username($post->ID)
This part is actually also working. So far my code looks like this:
$my_query = new WP_Query( array (
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
));
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$rated = check_rated_username($post->ID);
if (!$rated) : ?>
<?php //the_title() etc. Show post details (it's long so I'm not going to post the whole thing, but the post details are showing up fine) ?>
<?php next_posts_link('Next →', 10); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif; ?>
The problem next_posts_link() retrieves posts that meet the parameters set in the wp_query (and therefore are not part of the "if (!rated)" statement. There are instances when I click on "Next", and it shows a blank page--a post that this particular user has already rated.
How can I set it up so that I'm showing one post that the user has not rated on each page AND allow the user to navigate to the next unrated post by clicking on a NEXT button? I'm not married to the approach I have come up with. I just need to achieve this end result.
Thanks!
What you can do is first to fetch all posts id:s that a user has rate, then you can add an post__not_in in your query. It can look something like this:
<?php
/* Get all posts id:s that current user has rated */
$user_ratings = $wpdb->get_results( $wpdb->prepare( "SELECT rating_postid FROM {$wpdb->ratings} WHERE rating_userid = %d", get_current_user_id() ) ); ?>
<?php
/*Set up an empty array for save id:s in */
$post_not_in = array();
/* Check if $userRating has any values, if so loop thru and put the id into the array */
if($user_ratings){
foreach($user_ratings as $user_rating){
$post_not_in[] = $user_rating->rating_postid;
}
}
?>
<?php
/* The loop */
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'post__not_in' => $post_not_in
);
$my_query = new WP_Query($args);?>
The rest of your code
This way the loop only have posts that the current user haven't rated yet.

wordpress get_children doesn't reflect edits to image galleries

I have a very simple theme that retrieves all image gallery links for each post using the get_children() function.
Unfortunately, there's some strange bugs - first, if images are not uploaded when creating the post and are instead selected from the media library, they don't show up after publishing. Also, if I do any edit after a post has been created, be it reordering the images, adding new ones, deleting images, even deleting the whole gallery and creating a new one, they don't show up either. Refreshing browser cache doesn't do a thing.
If i change to one of the base themes, the images show up, and the right links are there when querying the db directly.
I reproduced the basic problematic code in a one file micro-theme, but the issue still happens:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php echo $post->id;
$args = array(
'numberposts' => -1,
'order_by' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => inherit,
'post_type' => 'attachment'
);
$images = get_children($args);
if ( empty($images)){
echo "nothing";
}
foreach ( $images as $id => $image ) :
echo $image->post_title;
endforeach;
?>
<?php endwhile; ?>
<?php endif; ?>
I'm stumped, I've tried using different functions to retrieve the posts with the same result, deactivated all plugins, nothing.
I'm testing both on OSX mavericks and an Ubuntu vps, could it be some database cache thing i'm supposed to flush somewhere?
When you set 'post_parent' => $post->ID, you will only get attachments to that page. That's part of the reason not all of the images show up.
But the bigger problem is that galleries are handled differently than posts/pages/attachments. Galleries only exist in the shortcode- they're not stored in the db- so you can't query them with get_posts or get_children.
To customize the way galleries are rendered you'll have to either hook into the wordpress code that creates the gallery, or deregister that function and write your own.
This question answers how to manipulate shortcode if you were writing a plug-in- but the process will be the same from a theme.
:-)

Wordpress: alphabetice posts using custom category template

I am trying to display posts ordered alphabetically by title, only for a certain category. I have tried to follow the instructions in the Codex but I am confused because my code in the template pages looks quite different from the examples in the Codex.
I have a category named "designers" so I duplicated the category.php and named it category-designers.php. Inside, there is a call to a loop-designers.php
Inside the category-designers, I have tried the Codex pice of code:
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args );
foreach( $glossaryposts as $post ) : setup_postdata($post);
get_template_part('loop', 'designers');
endforeach;
But the output is weird: first it displays a list of posts ordered by date, then it displays the same posts but ordered alphabetically, only that the alphabetically ordered list is repeated as many times as posts are (9 in this case).
I know I must be doing something terribly wrong but can't find examples using the get_template_part, they all use a foreach just like in the Codex.
Thanks for your answers.
Edited: in my loop-designers.php I have basically the same as in the loop.php, but with modifications so for that category no date, tags or other info are shown. I pasted the HTML here http://jsfiddle.net/6qdvF/
With the piece of code you have there you are including a loop in a loop. Using get_posts() with a foreach loop is essentially a WP loop, then you are including get_template_part('loop', 'designers'); which is more than likely another loop.
You can remove get_template_part('loop', 'designers'); from your piece of code and just stick in your tags to get the desired content, (i.e the_content(); the_title; the_excerpt; etc..) or you can create a new loop with wp_query() like the below example.
<?php
$query = new WP_Query(array('post_type' => 'post', 'cat' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
while ( $query->have_posts() ) : $query->the_post();
?>
// put your content template tags here
<?php endwhile; wp_reset_postdata(); ?>
This loop will query "Posts" and category 1. You can change 'post_type' => 'post', to query other post types by changing the word post to the post type name. Or change the category number to the desired category ID you wish to query.

How to get pagination working with “Pending” Posts

I'm working on a site that allows users to browse through pending posts/content on the front-end.
However, I can't seem to get pagination working with those posts. I've got a custom query that brings up the first page of pending posts on a category page, archive, etc.
But the Page 2, 3, etc. doesn't work.
Any thoughts?
Thanks!
Here's the example code I'm working with:
$args = array(
'cat' => $cat_ID,
'paged' => get_query_var('paged'),
'showposts' => 50,
'posts_per_page' => 50,
'post_status' => 'pending',
);
query_posts($args);
if( have_posts() ) : while (have_posts()) : the_post();
//Post code inserted here
<?php endwhile; ?>
<?php endif; ?>
WordPress pagination will 404 if there are not enough results in the main query to run to that page.
I'm sure there is a better way, but the only way I can think of round it is to use your custom loop in an archive/search page that has more posts than you have drafts.
For example, add your custom loop to the search.php template and reach it by passing a search query that will generate lots of results (e.g. 'a'). Using search.php will include pages, posts and custom post types in the results so will yield more results.
Use paginate_links for the pagination, which will continue to pass the queryvar on each page and after that you should be good.
On a slightly separate note, I would suggest using WP_Query instead of query_posts. See here for why.

Resources