I have woocommerce set up in a site and it has an ajax switch. With this the view of products changes from list to grid and vice versa. In the ajax call, I have queried the products again through a custom query using this code
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'product_cat' => $_GET['category'],
'paged' => $paged
);
$count = 0;
$loop = new WP_Query( $args );
But I dont get the required output from get_query_var(10). The url of the product page is as follows.
https:example.com/product-category/producers/arturo/page/2/?view=grid
Through this url all I need is the page parameter with value 2. How can I fetch it in the ajax call.
Related
I'm trying to configure the Wordpress pagination in de Index homepage.
I've used the WP_Query with the regard of get_query_var('paged') before, just as the code shows:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'cat' => 7,
'posts_per_page' => 2,
'paged' => $paged
) );
?>
Then I used the native Wordpress function previous_posts_link() to go back onte the list of posts.
The problem that happens is that when the page loads, the url of the page changes, but the posts that shows are the same as before. Even to page 3 or 4, it just shows the same list of pages.
Where am I going wrong?
I'm searching to exclude only the first post on an specific tag on Wordpress, using the PHP Array, because actually it excludes all post from that tag. Actually I have this code (I show only the PHP code, because the HTML code for the structure is irrelevant here):
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 8,
'post_type' => 'post',
'cat' => '14',
'tag__not_in' => array( 157 ),
);
$the_query = new WP_Query( $args );
global $wp_query;
// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $the_query;
I was trying with 'offset' => 1 but didnt worked.
Thanks.
Currently I am trying to get wordpress pagination to display at the bottom of the page but nothing shows up, if I echo out "paged" it outputs 1 so I believe it is correctly reading the page it is on. The loop I am using is as follows:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'company_list', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 6, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
To echo out the actual pagination at the bottom of the page I am using:
echo paginate_links( $loop );
Currently no pagination is being displayed at the bottom of the page but the loop is working correctly. The page is also not set as a static front page.
Thanks.
1)You do not have to use question mark in your first row
$paged = (get_query_var('paged')) ?
2)also see :
https://stackoverflow.com/a/33757648/10210277
I have a custom post type - collection
So then I add pages, inside those pages are shortcode to display woocommerce products that have same taxonomy
So for example on the collection page, I add a new page called "Jeans" and I tick checkboxes "denim", "faded", "worn-out" which are custom taxonomies
So on this page will display products that have those taxonomies
This is working fine currently
Now I display all the pages in "collection"
Jeans (3)
Slacks (5)
Shorts (2)
This is my problem now, I cannot count the number of products inside those pages
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'collection',
'order' => (isset($_GET['sort']) ? $_GET['sort'] : 'ASC'),
'orderby' => 'name',
'posts_per_page' => 100,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', 'collection' );
echo '<br>';
echo 'Number of products: '.$loop->post_count;
endwhile;
else :
get_template_part( 'no-results', 'archive' );
endif;
I am sorry I am still confused what to ask... :-(
you can use get_term for that.
you should use this code with your taxonomy
$term = get_term( 1, 'category' );
echo 'count: '. $term->count;
I have the following code in the "archive.php" file (from my current active theme):
list($f_categ_name) = explode('/', get_category_parents($cat));
if ( $f_categ_name == "X_CATEG") {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'category_name' => single_cat_title( '', false ),
'meta_key' => 'x_categ_type',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,
);
// get results
$the_query = new WP_Query( $args ); // The Loop
?>
<?php if( $the_query->have_posts() ): ?>
I have 2 categories : A (with 15 posts) and B (with 7 posts) and they have the same parent category(let's say X_CATEG). So basically I run the same code but $the_query->have_posts() returns true for A category and false for B category. WHY?
Using $GLOBALS['wp_query']->request I was able to debug the executed query and both queries (for category A and B) returned all post IDs.
In the WP_Query arguments, category_name will be category slug Not Category title.
single_cat_title functions will return the page title if a category or tag archive is queried. see reference
So You should use category slug in WP_Query arguments instead of category title.
Hope this will help.