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() ) :
?>
Related
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"
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;
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
I have (2) categories I want to exclude from the blog, how do I do this without a WP Plugin and by name not ID?
Here's the code:
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged); $exclude = get_cat_ID('feature');
$q = 'cat=-'.$exclude;
query_posts($q);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
Thanks for any help!
Try this:
$category_ids_to_ignore = array(3,4); // replace 3 & 4 with the actual catgory ids you want to exclude
$posts = new WP_Query(array('category__not_in' => $category_to_ignore, 'posts_per_page' => 5, 'paged' => get_query_var('paged')));
while($posts->have_posts()) : $posts->the_post();
endwhile;
SSomeone can tell me what's the best way to get a post using it's id?
I'am using this:
$query = query_posts('post_id='.$_GET['php_post_id']);
global $post;
foreach ($query as $post):
do stuff...
This is returning an array with all post
get_post( $post_id, $output );
So in practice will look like:
$my_id = 7;
$post_id_7 = get_post($my_id);
Further reference about the post's parameters and fields, here: http://codex.wordpress.org/Function_Reference/get_post
Update: It's the best practice when you need to get a single post by id, no cicles required.
Change post_id= to p=.
$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);
Click in this link to see: Retrieve a Particular Post
If you are looking to get a single post with an ID you already know or getting from another source, i'll suggest the below code.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'p' => $id, // id of the post you want to query
);
$my_posts = new WP_Query($args);
if($my_posts->have_posts()) :
while ( $my_posts->have_posts() ) : $my_posts->the_post();
get_template_part( 'template-parts/content', 'post' ); //Your Post Content comes here
endwhile; //end the while loop
endif; // end of the loop.
You can create a query like so:
$rd_args = [
'ID' => $postId
];
$query = new WP_Query($rd_args);
And then you can retrieve the post from the query. Or set it to the global query and loop over it:
$GLOBALS['wp_query'] = $query;
while ( have_posts() ) : the_post();
Here is the code to fetch post using query_post if you know the ID.
<?php
$my_query = query_posts('post_id=111&post_type=parks'); // in place of 111 you need to give desired ID.
global $post;
foreach ($my_query as $post) {
setup_postdata($post);
the_title();
the_content();
}
?>