I was tasked to display posts group by their categories, and I suppose I could do that with WP_Query. The problem is, wordpress still queries for the latests posts on the index page. How do I prevent that from happening?
I used the plugin MySQL Profiler to check what were being queried.
Why don't you just use custom query in index.php.
Something like this:
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'posts_per_page'=>5,
'orderby' => 'date',
'order' => 'DESC'
);
$temp=$wp_query;//save the main query
$wp_query=new WP_Query($args);
while ( have_posts() ) : the_post();
get_template_part(...);
endwhile;
$wp_query=$temp;//restore the main $wp_query
And to group posts by categories I would use this code from WordPress StackExchange.
Related
I am using JetPack's Infinite Scroll on the homepage of our site and it is working fine but I also want to display certain posts on there.
If a post is displayed outside the infinite loop I want it to be removed from the so there is no duplication.
I have a global array set up using bm_ignoreposts and have added it to the infinite loop render but for some reason it is not working. Here is the code.
Functions.php
The infinite renderer.
add_filter( 'infinite_scroll_archive_supported', 'tweakjp_custom_is_support' );
function mm_infinite_scroll_render() {
global $post;
while ( have_posts() ) : the_post();
bm_ignorePost($post->ID);
get_template_part( 'content-inf' );
endwhile;
}
The query
function mm_infinite_scroll_query_args($args) {
$new_args = array(
'posts_per_page' => $args['posts_per_page'],
'paged' => $args['paged'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => array( 'post', 'features', 'it_hardware', 'videos' ),
'post_status' => 'publish',
);
return $new_args;
}
For reference here is the ignore posts code:
function bm_ignorePost ($id) {
if (!is_page()) {
global $bmIgnorePosts;
$bmIgnorePosts[] = $id;
}
}
I decided to not use JetPack anymore and go for a custom Javascript version which uses AJAX to load new posts running a new query.
After speaking to Jetpack support, they believe it is not possible based on how the plugin works.
When i visit mysite.com/category/*** It only displays last post of that category.
But i want it to display recent ten posts of that category.
In short...
I want my site http://bishwash.com.np/category/entertainment/ to display posts like http://www.onlinekhabar.com/category/bichitra-world/
you can get the recent posts with wp_recent_posts($arg,ARRAY_A );
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
You can use get_posts to retrieve posts.
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&category=1');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Change category=1 to your category ID number.If you to get post from XYZ category and XYZ category had 13 ID number then it would be something like this category=13.
In categroy.php page before
<?php if ( have_posts() ) : ?>
//paste the following code
$catID = the_category_ID();
$args = array( 'numberposts' => '10','category' => $catID);
$recent_posts = wp_get_recent_posts( $args );
setup_postdata($recent_posts);
At the end of loop
wp_reset_postdata();
The number of posts is set globally for all index type pages (including the home page and archives and category) in settings, reading
It seems you have limited it to 1 ? Then it's better design to set it to 10 because that will apply to all pages, and design a specific template for the home page, where you limit the number of posts with the methods given to to.
You can achieve that in two ways :
- build a page template, with a specific query, create a page with this template, and define it as homepage in settings -> reading
- create a home.php file in your theme
In both cases, it's better to have a child theme, not to loose modifications when your theme is updated.
Create the template (sample.php) and write code like
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
or use CATEGORYNAME
Note: When we click the link it redirects to to the sample.php and the category name is dynamic that means when we click the category its automatically stored to the CATEGORYNAME
I have a custom query which I'm trying to run. How I want it to work is,
Firstly, check the page title
if the page title is the same as the name of the cat from a custom taxonomy then show all the posts that are in that custom taxonomy - post type.
The only trouble is, it isn't returning anything, I've made sure I'm targeting the correct 'post type' & correct 'taxonomy' name. It will return the cat id with the following:
$cat->cat_ID;
But won't return any posts, here is my code:
<?php
// Get the name of the page
$theTitle = get_the_title();
//Get the taxonomy for the custom post type
$categoryselect = array( 'taxonomy' => 'team-members' );
$categories = get_categories($categoryselect);
// loop through each category as cat
foreach ($categories as $cat):
//If cat is the same as title *name* then lets do something
if($theTitle == $cat->cat_name):?>
<h3>We’re here to help.</h3>
<?php
$catID = $cat->cat_ID;
//echo $catID;
//query the posts but, use the cat ID so the page relates to it.
$args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
$loop = new WP_Query( $args );
// run the loop for posts
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="person">
<h5><?php the_title(); ?></h5>
</div>
<?php endwhile;
endif;
endforeach;
This is on a page.php template
Any suggestions?
You have a couple of issues here. Lets start with this line
$args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
query_posts should never be used, and nor should you make use of two queries in one
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly showposts has been depreciated in favor of posts_per_page
You terminology is incorrect, and therefore you are making use of wrong parameters in your query. You are not working with categories here, but with a custom taxonomy and terms. To get a good perspective of categories, terms and custom taxonomies, see this post I have done on WPSE
You should make use of a tax_query and not the category parameters in WP_Query
To come back to how you get your terms. The way you are doing it with get_categories() is not wrong, but it can become confusing as you are actually working with a custom taxonomy and not the build-in category taxonomy. I would suggest to use get_terms() instead
I actually feel that you don't need to use get_terms, get_categories or the foreach loop. I have checked your code and it seems the only time that something will show is when the term name is equal to the page name.
You already have the taxonomy name and the term name, the only thing that you can maybe do is to check if the term exists, and then feed that to your custom query
This is a modified version of your code, UNTESTED
<?php
// Get the name of the page
$theTitle = get_the_title();
//Get the taxonomy for the custom post type
$taxonomy = 'team-members';
//Set the page title as term name
$term = $theTitle;
if( term_exists( $term, $taxonomy ) ) : // Check if there is a term that match the page title ?>
<h3>We’re here to help.</h3>
<?php
//query the posts but, use the cat ID so the page relates to it.
$args = array(
'post_type' => 'team',
'orderby' => 'title',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'name',
'terms' => $term,
'include_children' => false
),
),
);
$loop = new WP_Query( $args );
// run the loop for posts
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="person">
<h5><?php the_title(); ?></h5>
</div>
<?php endwhile;
endif;
?>
EDIT
Have now tested the code and made a few minor adjustments. It is working 100% now on my local install.
I'm trying to query posts in wordpress, so far so good. However, I would like to show a post with a specific tag 'info' in front of all (even in front of sticky).
Now the query of the theme is like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
if(of_get_option('sticky-posts') == 'show_sticky'){
query_posts (array( 'post__in' => $sticky, 'order' => $order_posts, 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
}
And after that the loop starts:
//BEGIN LOOP
//=====================================================?>
<article id="articlehold">
<?php
if(have_posts()) : while ( have_posts() ) : the_post();
Now I've done some editing in the query to get the post with the 'info' tag in front, but no luck. Now I'm not sure where the best place is to edit this, in the loop or before the loop?
How could I do this?
I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields.
Here's the current code I'm using that hides it nicely:
while ( have_posts() ) : the_post();
$is_taken = get_field('taken_check', $this_id);
if ($is_taken!=1) {
get_template_part( 'basket_selection' );
}
endwhile;
However, it simply just hides the post but still considers it as a post on the "posts_per_page" function.
For example, There are 20 posts in total and I've set the limit to 10 posts per page. If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2.
Is there a way to simply just ignore the hidden posts and not count it as a "post"?
Try this:
Apply Custom Fields Parameters in get_post query itself.
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => '<YOUR_POST_TYP>',
'meta_key' => 'taken_check',
'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));
Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts
I've managed to solve it by changing the get_posts to wp_query within the category.php.
I first added this code to detect the current category viewed and filter the query to only display taken_check = 0.
$this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
foreach ($this_cat as $this_cat){
$this_catid = $this_cat;
break;
}
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'cat' => $this_catid,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'taken_check',
'value' => '0',
)
)
);
$wp_query = new WP_Query($args);
I then just continued with the default loop sequence. The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. Still puzzled as to why I can't just use $this_cat[0] since it's an array. It keep returning blank.
Oh well, but it works now with pagination, so I'm happy :)
Thanks for all the help!