I need a bit help from you. I have a custom search engine to search products from a post type taxonomy :
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
This query outputs about 60 products with pagination (10 products per page), but when user visits the page without using the search engine, all products should be displayed. Instead, the $_SESSION remains and display only the previous results.
I just want the pagination working when I do search, and all products displayed when I access the page without using the search engine.
Does any Wordpress expert have an idea ?
Thank you.
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
else {
$args = array(
'post_type' => 'product',
'showposts' => -1,
)
$posts = new Wp_Query($args);
}
simply put an else block
CODE ,
global $post;
$id = intval($_GET['cat']);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$products = new WP_Query( array(
'post_type' => 'products',
'order' => 'ASC',
'posts_per_page' => 5,
'paged' => $paged
) );
endwhile;
HTML ,
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $products->max_num_pages ); ?></li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
Related
I've got this code:
<?php wp_dropdown_categories(); ?>
And I've got this code from Codex:
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
First function takes all categories, and second function displays them on select. When I select one of the categories, then I'm redirected to the URL of the chosen category.
My problem is, that loop doesn't show posts from chosen category. Searching for a solution, I came across something like this:
$query = new WP_Query( array( 'category_name' => 'staff' ) );
But it works only for things like "XYZ Category page". My page allows creating new categories by end user, so I need something more dynamically.
Maybe something like this?
$cat = get_the_category();
$query = new WP_Query( array( 'category_name' => $cat ) );
and then use it in loop?
EDIT: This is my code that I used in loop (both category.php and archive.php
<?php
query_posts(array('posts_per_page' => 2, 'paged' => $paged));
$queryObject = new Wp_Query( array(
'posts_per_page' => 2,
'post_type' => array('post'),
'paged' => $paged,
'orderby' => 1,
));
if ( $queryObject->have_posts() ) {
while ( $queryObject->have_posts() ) {
$queryObject->the_post();
?>
<div class="shorts">
<div class="shorts1">
<a class="Text3" href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
<br><br>
<a class="Text2"><?php the_excerpt() ?></a>
<br><br>
<div class="more-wrapper">
<div class="more">
Dowiedź się więcej
</div>
</div>
</div>
</div>
</article>
<?php }} ?>
You don't really need to define a custom query in category.php . WordPress is clever enough to do that for you. Just replace
<?php
query_posts(array('posts_per_page' => 2, 'paged' => $paged));
$queryObject = new Wp_Query( array(
'posts_per_page' => 2,
'post_type' => array('post'),
'paged' => $paged,
'orderby' => 1,
));
if ( $queryObject->have_posts() ) {
while ( $queryObject->have_posts() ) {
$queryObject->the_post();
?>
With
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
?>
$categories = get_query_var('cat');
if (!empty($categories))
{
$category = get_category($cat);
$categoryName = $category->name;
$categorySlug = $category->slug;
$catId = $category->cat_ID;
}
$args = array(
'posts_per_page' => 12,
'post_type' => 'post',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
array (
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $categorySlug
)
));
$query = NEW WP_Query($args);
I have a Parent page in Wordpress and am using the below code to display information about each of the child pages the parent has. Perfect.
What I need to do elsewhere on the page is display the count of child pages, for example 'This page has X child pages'.
Can anyone help me to do this please?
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
//content goes here
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can use like
$pages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'property'));
$count = count($pages);
I am currently getting all the post from the db via a query. The php code included is part of a custom Wordpress template. I am using custom meta boxes from: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
what do i need to do to paginate every 6 post?
<?php
//get_template_part( 'content', 'page' );
echo'<h2 class="section-header-dark">'.get_the_title().'</h2><hr>';
//adjusting the query
$args = array(
'post_type' => 'blog',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => ASC
);
// The Query
$latest_post = new WP_Query( $args );
// The Loop
if ( $latest_post->have_posts() )
{
while ( $latest_post->have_posts() )
{
$latest_post->the_post();
$desc = wpautop( get_post_meta( get_the_ID(), '_cw_desc', true ) );
echo'<div class=""><h5 class="">'. get_the_title(). '</h5>';
echo $desc;
echo'<h6 class="news_date f_right"><i>'. get_the_date(). '</i></h6>';
echo'</div><hr>';
}
}
else
{
// no posts do nothing
}
wp_reset_postdata();
?>
You need to set 'posts_per_page' => 6 and add the paged Paramter to the query.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged
);
?>
Visit http://codex.wordpress.org/Pagination for more details.
I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/
I'm having difficulty pulling only the desired category into an archive page on Wordpress. My code is below, I thought by defining get-category-by-slug this would work, but instead its pulling all of the post from every category into the page.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post();
?>
Any ideas on how to fix this would be appreciated.
I've also tried these combinations with no luck.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
and
<?php $query = new WP_Query( 'category_name=weddings' ); ?>
and
<?php
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
Please do not use query_posts its not the best way to query data its a last resort if you will.
Instead use WP_Query to get posts from one category just do $query = new WP_Query( 'category_name=staff' ); refer to this page for more information on how to get posts from one category using WP_Query.
EDITED
Try this
$the_query = new WP_Query( array(
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'wedding', //name of category by slug
'order' => 'DESC',
'posts_per_page' => )); // how many posts to show
// Put into the loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
// Restore original Post Data if needed
wp_reset_postdata();