woocommerce displaying products of a specific page - woocommerce

First sorry for my english, is not my first language.
So what I want to do is to get the products of a particular page, using the get variable, so this is my current code...
$page = 0;
if(isset($_GET['pagina'])){
$page = $_GET['pagina'];
}
$args = array( 'post_type' => 'product','posts_per_page' => 1, 'page' => $page);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo get_the_title();
endwhile;
I though the parameter 'page' => $page will do exactly that... but it seems I was wrong and maybe I understood it wrong when I was reading it.
So example if I have 2 products: product_1 and product_2 and my posts_per_page is 1 what Im trying to archive is that
if I type http://myurl.com/?pagina=1 will show product_1
and if I type http://myurl.com/?pagina=2 will show product_2
Thanks in advance
EDIT: I think my english failed to communicate my problem so ima try explaning in other example
What I want is to get the products that suppose to go in the page I get tru $_GET
So if I have to use my own pagination, it will look something like this:
$page = 1;
$posts_per_page = 1;
if(isset($_GET['pagina'])){
$page = $_GET['pagina'];
}
$start_from = $posts_per_page * ($page-1);
$sql = "SELECT * FROM Products LIMIT $start_from,$posts_per_page"

Related

BuddyPress - How to display posts created by friends?

In WordPress with BuddyPress, how can I display for a user posts (custom post type) that are created by his friends only?
Not as/in activity steam (it's disabled on the site), but as usual loop while(have_posts()):.
Thank you in advance.
Try:
$friend_ids = friends_get_friend_user_ids( bp_loggedin_user_id() );
if ( ! empty( $friend_ids ) {
$args = array(
'author__in' => $friend_ids
);
$query = new WP_Query( $args );
}
Adjust $args to include other parameters like post_type. And then create a while loop.

query_posts year parameter returns 0 posts

I have a custom post type displaying posts in the archive-CPT-name.php template file and need to filter posts by year. When the query_var archive is present, the below code is added prior to the loop:
global $query_string;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$archive_year = get_query_var('archive', false);
query_posts( $query_string . '&year='.$archive_year.'&paged='.$paged );
This returns 0 posts. If I change year='.$archive_year.' to day=16, keeping everything else the same, then I correctly get the posts created on the 16th. I'm also able to filter by w and monthnum successfully. It's only the year parameter that doesn't work.
Any ideas why this might be happening?
Edit: $wp_query->request includes the following SQL ... WHERE 1=1 AND ( YEAR( wp_posts.post_date ) = 2016 ) AND ( 0 = 1 ) .... Why is the AND ( 0 = 1 ) being included? Where might that be coming from? It looks like it may be the cause of my issue.
Update: I'm using the following code as a workaround, but would rather figure out why the above code doesn't work.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$archive_year = get_query_var('archive', false);
$args = array('post_type' => 'repair-post', 'date_query' => array(array('year' => $archive_year)), 'paged' => $paged);
$loop = new WP_Query( $args );
if ($loop->have_posts()) :
while ( $loop->have_posts() ) :
?>

Woocommerce get product sku for single product page in functions.php

I'm having a lot of trouble getting the product SKU for a product in a single product page inside of functions.php. I have multiple single product pages and I want different text to display depending on the product. I've created a child theme and I'm working in the functions.php file. I'm new to wordpress and editing themes so I don't quite understand the order of operations yet. I was able to get code working to loop through and give me all the SKU's for all the products but that's independent of the actual page that I'm on.
I've tried a bunch of things. The common solution seems to be:
global $product;
echo $product->get_sku();
but that doesn't work. For some reason the $product variable is null inside the functions.php script.
The loop that I have loops through all the product posts and gets the post ID. I also tried getting the ID of the current page but was also unsuccessful in doing that (the code below was copied from another site). Any help would be greatly appreciated. Thanks.
$full_product_list = array();
$loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
get_sku() works just fine assuming that your theme has not removed woocommerce_after_single_product_summary for any reason.
add_action( 'woocommerce_after_single_product_summary', 'so_32545673_add_sku', 35 );
function so_32545673_add_sku(){
global $product;
if( $product->get_sku() ) {
echo 'the sku = ' . $product->get_sku();
}
}
I didn't see the output at first because it was below all the related products and upsells, etc.
Additionally you may want to switch to wc_get_product() instead of trying to call the class directly. With the following I get a list of product SKUs. You should use an if( $loop->have_posts() ) to open the <ul> but I'm being lazy.
$loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
$product = wc_get_product($theid);
if( $product->get_sku() ) echo '<li>' . $product->get_sku() . '</li>';
endwhile;

pods: Query customs posts filter by custom taxonomy

Using pods, I've created a new custom post type named staff_member which contains a field staff_department_relationship of type Relationship that is related to a custom taxonomy staff_department. Also note that the staff_department_relationship field is a single select of format dropdown.
Now I'd like to query all staff members from a particular department and have tried a lot of different things as per http://pods.io/docs/code/pods/find/ but without much success. I sometimes get a DB error or no posts at all:
$params = array(
'limit' => -1,
// 'where' => 'staff_department_relationship.staff_department = "some_custom_taxonomy_slug"'
'where' => 'staff_department.name = "some_custom_taxonomy_slug"'
);
$pods = pods('staff_member', $params);
Anyone with any idea what's going on?
Defines slug instead of name
'where' => 'staff_department.slug = "some_custom_taxonomy_slug"'
Code List post_type for taxonomy - PODS Wordpress
<?php
$params = array(
'limit' => -1,
'where'=>"TaxonomySlug.Slug = 'TaxonomyTermSlug'" ,
);
$pods = pods( 'NamePostypeHere' )->find( $params );
if ( $pods->total() > 0 ) {
while( $pods->fetch() ) {
//reset id
$pods->id = $pods->id();
//get the template
$temp = $pods->template( 'NameTemplateHere' );
//output template if it exists
if ( isset( $temp ) ) { ?>
<?php echo $temp; ?>
<?php }
}
//pagination
echo $pods->pagination();
}
else { echo 'No content found.'; }
?>

query_posts worked when wp_query didn't -- trying to understand (WordPress)

This is an odd question, in that I've already found a working solution. But: I'd really like to understand why it worked, and why what seemed like it should have, didn't. If you can explain why I'm an idiot, I'll be grateful.
The task: modify the main loop to exclude a category, and only show two posts per page.
Here's what worked:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-17,-18&posts_per_page=2&paged=$paged");
?>
Here's what didn't:
<?php $query = new WP_Query( 'cat=-17', 'posts_per_page=2' ); ?>
Nor did this:
<?php $query = array (
'cat' => -17,
'posts_per_page' => 2
);
$queryObject = new WP_Query($query);
?>
So: What's going on here?
Many thanks,
-m
There are a couple of reasons why it doesn't work:
1) $query = new WP_Query( 'cat=-17', 'posts_per_page=2' ); didn't work because you're attempting to pass 2 arguments. It needs to look like this, if you're doing a string query:
$query = new WP_Query( 'cat=-17&posts_per_page=2' );
2) Your second query is correct, but the reason why it's "not working" is more than likely your subsequent loop - which you've left out of your code. When you're using query_posts, you're modifying the global $wp_query object, so your loop can just look like this:
if(have_posts()) : while(have_posts()) : the_post();
//Do stuff....
endwhile;endif;
But using a new instance of WP_Query it needs to look different. Here's your fixed code:
<?php
$query = array (
'cat' => -17,
'posts_per_page' => 2
);
$queryObject = new WP_Query($query);
if($queryObject->have_posts()) : while($queryObject->have_posts()) : $queryObject->the_post();
//Do stuff....
endwhile;endif;
More on WP_Query here:
http://codex.wordpress.org/Class_Reference/WP_Query

Resources