I have written code in content-single.php It is searching pages also.I want to search only post when I am Searching from post
https://wordpress.stackexchange.com/q/147222/50884
From what i understand you need to have your WordPress search only search posts for results, you can insert this code before the ‘if(have_posts()):’ line on search.php and it will do that for you.
// search only posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
Further to that you can add any arguments from the standard WP_Query function to there to make the search more custom :)
Related
I have a Custom Post Type called portfolio and a custom taxonomy portfolio_category that has a term called narrative.
I'm able to access the archive page using the URL /portfolio-category/narrative/.
I want the homepage to display all the items the same as on the narrative archive page without using a redirect.
I've added the following to functions.php
function custom_front_page($wp_query){
if($wp_query->get('page_id')==get_option('page_on_front')){
$wp_query->set('post_type','portfolio');
$wp_query->set('page_id',''); // empty
// fix conditional functions
$wp_query->is_page = false;
$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true;
}
}
add_action('pre_get_posts','custom_front_page');
This is displaying all of the portfolio items on my homepage, but I would like it to be just showing the narrative items.
I've tried this in the php template file, but it isn't working either
<?php
$custom_query_args = array(
'post_type' => 'portfolio',
'portfolio_category' => 'narrative',
);
$custom_query = new WP_Query( $custom_query_args );
?>
How can I get just the narrative items for to show on the homepage?
You're on the right lines but you are not searching by custom taxonomy correctly. Searching by custom taxonomy is different than searching by categories.
If you take a look at the WP_Query documentation for searching by taxomony, you will see that you need to use tax_query to search posts for a custom taxonomy term.
You can use the code below in the template file you use for your homepage. The comments explain what each part does:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 5, // number of results to get, or -1 to get all of them
'tax_query' => array(
array( // important note, this is an array inside the tax_query arrays
'taxonomy' => 'portfolio_category', // your custom taxonomy
'field' => 'slug', // what to search, e.g. slug, term_id, name
'terms' => 'portfolio' // the term(s) to search for. If you want to search for multiple terms, you can use an array
)
)
)
// do a new query with your arguments
$portfolio_query = new WP_Query($args);
// if the query returns any results, loop through them and process them as required
if( $portfolio_query->have_posts() ):
while ( $portfolio_query->have_posts() ) :
$portfolio_query->the_post();
// do stuff with the post here, e.g. display the post...
endwhile;
endif;
// IMPORTANT! The custom query will overwrite the $post values that are used in the the_post etc.
// This restores the the current post in the main query - i.e. your homepage post.
wp_reset_postdata();
These apis work perfectly.
http://domain.com/wp/api/get_tag_posts/?tag_slug=banana
http://domain.com/wp/api/get_category_posts/?category_slug=featured
Is there any way to join multiple tags or categories into a single request?
E.g.
http://domain.com/wp/api/get_category_posts/?category_slug=featured,news
or
http://domain.com/wp/api/get_category_posts/?category_slug=featured|news
I'm hoping to do what in SQL would be a "where category_name in ('featured','news')
You are after this
<?php $args = array(
'posts_per_page' => 10,
'category__not_in' => array(5,151)
);
query_posts($args);?>
This is what you need to do in PHP to obtain multiple posts from multiple categories. Now in case you are also looking for json or xml or any format it spits out. Put a new function in functions.php and register it with
add_action( 'wp_ajax_nopriv_getmyjson', 'myfunctionname' );
add_action( 'wp_ajax_getmyjson', 'myfunctionname' );
function myfunctionname()
{
Global $wpdb;
...
}
Call this in your theme or plugin and use action=getmyjson and url goes to admin_ajax with nonce set. After Global $wpdb you can use above function to bring all posts and then through them out as json object. Something like this
$response = json_encode( array(
'success' => true,
'message' => $ajaxmsg
'posts' => $mypostarray
)
);
// response output
header( "Content-Type: application/json" );
echo $response;
die(); //This would then make sure it is not getting anything else out of wordpress and sends only json out.
Once this is all done. You will have multiple posts out put in json format.
I needed to do the same thing and ended up using this plugin
Query Multiple Taxonomies. This plugin lets you do faceted search using multiple custom taxonomies. (author's description).
http://wordpress.org/extend/plugins/query-multiple-taxonomies/
It's got a handy sidebar widget also. Hope this helps!
Have you tried making the category_name an array? e.g.
<?php $args = array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news')
);
query_posts($args);?>
Also, with the tag, I found this example:
<?php query_posts('cat=32&tag=hs1+hs1&showposts=10'); ?>
or
<?php query_posts(array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news'),
'tag_slug__and'=>array('banana')
)); ?>
I'm working on a site that allows users to browse through pending posts/content on the front-end.
However, I can't seem to get pagination working with those posts. I've got a custom query that brings up the first page of pending posts on a category page, archive, etc.
But the Page 2, 3, etc. doesn't work.
Any thoughts?
Thanks!
Here's the example code I'm working with:
$args = array(
'cat' => $cat_ID,
'paged' => get_query_var('paged'),
'showposts' => 50,
'posts_per_page' => 50,
'post_status' => 'pending',
);
query_posts($args);
if( have_posts() ) : while (have_posts()) : the_post();
//Post code inserted here
<?php endwhile; ?>
<?php endif; ?>
WordPress pagination will 404 if there are not enough results in the main query to run to that page.
I'm sure there is a better way, but the only way I can think of round it is to use your custom loop in an archive/search page that has more posts than you have drafts.
For example, add your custom loop to the search.php template and reach it by passing a search query that will generate lots of results (e.g. 'a'). Using search.php will include pages, posts and custom post types in the results so will yield more results.
Use paginate_links for the pagination, which will continue to pass the queryvar on each page and after that you should be good.
On a slightly separate note, I would suggest using WP_Query instead of query_posts. See here for why.
On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.
Here is the code I am using to dump all the attachments:
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_attachment_link($post->ID, true);
the_excerpt();
}
}
?>
Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?
I've done something similar using the 'offset' parameter for get posts.
Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.
Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.
i have a specific single page which display a single post.
the thing is that i want to display below it all the other posts which have the same special meta data and i made that to work as well.
the problem starts when i try to make pagination to the list of the posts below.
the single post url is something like that:
blog.com/somepost
and the pagination link to the second page of posts below looks someting like this
blog.com/somepost/page/2
and wordpress automatically redirects me back to
blog.com/somepost
how can i prevent it from redirecting me back?
btw, i"m using something like that:
i"m doing something like this:
while( have_posts() ): the_post();
//here printing the single post
endwhile;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_btree_project_id',
'meta_value' => $post->ID,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
while( have_posts() ): the_post();
//looping through the related posts here
endwhile;
The reading I've done about WordPress pagination gives me the impression that it is an imperfect feature. It requires the global var $wp_query which stems from the WP_Query object. WP_Query holds the global $wp_query which is necessary for making even basic pagination work. Custom queryies don't have access to $wp_query, nor do they own a var to control pagination. I assume you are using a custom query to grab that single post, and as this article points out, with custom queryies:
the “fix” is to trick WordPress into
using the global $wp_query variable
when using our own custom loops.
The article gives an example of utiilizing the global var in your custom query so that you have access to the query_vars that make pagination possible.
I expect that your permalink structure and a custom query that I'm guessing you are using might not be working because the global $wp_query var isn't available during your loop to show related posts.
What does your code to get, display, and paginate the related posts look like? Can you post?