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);
Related
I am trying to query several different types of post on the same page. When I try to query the second time (Essays), nothing shows up, meaning my if ($arr_posts->have_posts()) is evaluating as false. The first query and the third and fourth query are working fine. And the second query was working until I added this Interview query before it. And even when I commented it out, it still stopped showing up. What am I missing? And mwp_interview is a custom post type.
<!--Latest Interview-->
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY INTERVIEW POST
</div>
<?php
endif; ?>
<!--Latest Essay-->
<?php
wp_reset_query();
wp_reset_postdata();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Essays in Discipleship',
'posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY ESSAY POST
</div>
<?php
endif; ?>
<!--Latest Special Series-->
<?php
$ss_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Special Post',
'posts_per_page' => 1,
);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );
if ( $ss_arr_posts->have_posts() ) :
$ss_arr_posts->the_post();
?>
<div>
DISPLAY SPECIAL SERIES POST
</div>
<?php
endif;
?>
<!--Latest Podcast-->
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Podcast',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY PODCAST
</div>
<?php
endif;
?>
As per the Wordpress documentation WP_Query Category Parameters.
category_name (string) – use category slug.
The argument's name category_name is actually misleading, category_name is referring to the category SLUG, NOT the actual category NAME.
<?php
$args = [
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo "<hr>";
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'essays-in-discipleship', // ... must be set set to "Essays In Discipleship" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'special-post', // ... must be set to "Special Post" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>
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>
I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );
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 have following code
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 3,
'cat_id'=> 5,
));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I want to show category whose id is 5 on my bog page. But this is showing other categories as well like 4 & 3.
Similarity issue is for archive page as well. Where i am wrong?
check this, i think this is helpful for you.
<?php
$posts = get_posts('category=5&orderby=rand&numberposts=5');
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php } ?>
Please try with below code :
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
this is working at my end
Try this
<?php
$args3 = array(
'numberposts' => 1,
'cat' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$homepage_post = get_posts($args3);
foreach ($homepage_post as $post) : setup_postdata($post);
// write your code here ...
endforeach;
wp_reset_postdata();
?>
--
Thanks