I've set up a custom search to use the s GET variable on a URL. I want it to fetch certain results but I've come across an odd problem. First, here's my code:
$search_term = $_GET['s'];
if($search_term!=''){
$s = new WP_Query(array('s' => $search_term));
$search_array = array();
if($s->have_posts()){
while($s->have_posts()){
$s->the_post();
$title = get_the_title();
$permalink = get_permalink();
$search_identifier = $title.$permalink;
array_push($search_array,$search_identifier);
}
}
}
Essentially, I'm creating an array of unique values for each post because I need to use them for a function immediately after that.
I have a post titled 'Kitchen Assistant.' When I search 'kitchen', it shows up. When I search 'assistant' it does not show up.
I have other posts with the word 'assistant' in the title and content and those show up when I search 'assistant'. I'm curious as to why it would show up with one search term and not the other?
I've used var_dump($s) and the post is in the dump when I search 'assistant' but not when I search 'kitchen.'
Any help would be greatly appreciated. Thanks!
Try this.
$search_term = $_GET['s'];
if(!empty($search_term)){
$args = array(
'post_type' => 'post',
'meta_query' => array(
'key' => 'title',
'value' => $search_term,
'compare' => 'LIKE'
)
);
$s = new WP_Query($args);
$search_array = array();
if($s->have_posts()){
while($s->have_posts()){
$s->the_post();
$title = get_the_title();
$permalink = get_permalink();
$search_identifier = $title.$permalink;
array_push($search_array,$search_identifier);
}
}
}
I figured out the answer to my own question (finally!). I'm using the NineToFive template and I guess by default there's a limit on the number of search results that's returned (I'm not sure if that's a WordPress default; maybe someone can clarify that).
I changed my query to this:
$s = new WP_Query(array('s' => $search_term, 'posts_per_page' => -1));
To not put a limit on the search results and that changed it. I needed to find every possible search term on page 1 in order to cross reference it with the location but it was 'paginating' the search results in search query end of it.
Thanks for the help!
Related
I want to display the total number of found posts on results page in WordPress. For example "If someone searches for 'cat' it should display count for posts with the term 'cat', Right now I inserted some PHP code but it is displaying the total number of published posts and that's not I want to achieve. Please follow this link: https://bigfunx.com/?s=cats&id=1516&post_type=post
And you will clearly see that there are four articles found for cat and it is displaying 28 as count instead of 4. I hope you are understanding what I am trying to achieve. Looking forward to hearing from you soon. Thank you!
I try to enter the following code:
<?php $args = array(
'post_type' => 'post'
);
$query = new WP_Query($args);
$totalposts = $query->found_posts;
if($totalposts < 2) {
$result = "Meme";
} else {
$result = "Memes";
}
echo $totalposts . " " . $result; ?>
The above code is displaying the total number of published posts and also with singular and plural feature but I know I am missing something to properly displaying the result, Your help will be highly appreciated.
I am not sure where you add that code, but you can try this:
$args = array( 'post_type' => 'post', 's' => isset( $_GET['s'] ) ? $_GET['s'] : '')
Just add the 's' key in you $args variable.
This is not the best solution because you are making additional query on the search page. But given the info you have in your question that should work.
Better solution would be in the page where you display the title (search page) you can try:
global $wp_query; $totalposts = $wp_query->found_posts;
As mentioned in the comments, use th _n() to pluralize your string.
<?php
$args = array(
'post_type' => 'post',
);
$query = new WP_Query( $args );
$totalposts = $query->found_posts;
echo esc_attr( $totalposts . ' ' . _n( 'Meme', 'Memes', $totalposts ) );
I'm sure this is going to be a silly question... but here it goes.
I currently have a page that only displays posts of a Custom Post Type (car).
I do this by running a query
$args = array(
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
For this Custom Post Type i have a Custom Taxonomy e.g. Subaru, Honda etc...
I'm just trying to work something else out, but if I wanted to show only posts that are Subaru's, how would I query that?
I guess I want to query the 'slug' (subaru), this code doesn't work, but you can see the route I was heading...
$args = array(
'name' => 'subaru',
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
I know name isn't right. What is the correct term to add to my $args array?
Many thanks
Depends on what your taxonomy is called. In my example its called 'brands':
$args = array(
'post_type' => 'car',
'brand' => 'subaru'
);
$query = new WP_Query( $args );
see http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
What you're trying to do is a taxonomy query. I could tell you how to do that but I think it's important I point out a much bigger mistake first. You shouldn't querying this on a page.
That's what archives are for.
Create a template file called archive-car.php and output there instead. That removes the need to run a custom WP Query.
Then create another file taxonomy-{your-custom-taxonomy-name}.php
Output the cars there as well and your problem is solved without adding any inefficient queries.
I think you need to use get_terms()
$terms = get_terms("Subaru");
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
See this http://codex.wordpress.org/Function_Reference/get_terms
I've got some custom post types set up in Wordpress using Pods and linking them using relationship fields. Now I'd like to display (and link to) the related custom posts 'postB' from a single post 'postA'. I also just want to display those posts which got a date in the future, which is also stored in a custom field in 'postB'.
This is what I've currently got so far, put into a theme template file (single-posta.php):
<?php
$id = get_the_ID();
$params = array(
'where' => 'postB.ID IN ('.$id.')',
'limit' => -1, // Return all
//'oderby' => 'postB.customDate, (order is not working, so I commented it out)
//'order' => 'ASC'
);
$postsB = pods( 'postB', $params );
if ( 0 < $postsB->total() ) {
while ( $postsB->fetch() ) {
?>
<p>
<?php echo $postsB->display( 'title' ); ?><br>
<?php echo $postsB->display( 'customDate' ); ?><br>
</p>
<?php
}
}
?>
So how can I
order the results?
link to these posts?
limit them to dates in the future?
Btw. is this the right way to get those posts anyway?
You could use WP_Query too, but since you're using the Pods find() syntax, I'll give you the correct code for what you're after using that:
$params = array(
'where' => 'postB.ID IN ('.$id.')',
'limit' => -1, // Return all
'orderby' => 'customDate.meta_value ASC'
);
$postsB = pods( 'postB', $params );
Pods doesn't let you create fields with capital letters though, so it's likely you created that one outside of Pods, correct? Just double checking, if it was created with Pods it would be named 'customdate'
I am creating a newspaper website that will have Volumes and Issues. Volumes increment yearly, issues increment weekly. I have created a custom post type of article with a taxonomy of issue.
Currently the code below will get the most recent post from the article with the most recent issue taxonomy. I want it to get all of the posts from the most recent issue. I figured out I can get the next post by changing $issue[0]->slug to $issue[1]->slug. I realize I just need a loop but I cant quite figure it out.
Your help is appreciated.
<?php
$issue = get_terms('issue','orderby=none&order=DESC');
$latest_edition = $issue[0]->slug;
query_posts('&post_type=article&gdsr_sort=thumbs&gdsr_order=desc&issue='. $latest_edition) . '&showposts=99'; ?>
You're not looping through your posts. You need to do something like:
// Returns an array issues
$issue = get_terms('issue','orderby=none&order=DESC');
// You want the most recent issue
// i.e. that which has an array key of 0
$latest_edition = $issue[0]->slug;
// Return all the posts for this issue
query_posts('&post_type=article&gdsr_sort=thumbs&gdsr_order=desc&issue='. $latest_edition) . '&showposts=99';
// Loop through each post
while ( have_posts() ) : the_post();
// Echo out whatever you want
echo '<li>';
the_title();
echo '</li>';
endwhile;
Thx for the answer #hohner.It really helped me to go ahead with my issue. #Aaron Snyder for the complete result you can add some loop with your taxonomy information like this for showing all posts results
$latest_edition = $issue[0]->slug;
$latest_edition = $issue[0]->term_id;
$postsart = get_posts(array(
'showposts' => -1,
'post_type' => 'articles',
'tax_query' => array(
array(
'taxonomy' => 'articles-tax',
'field' => 'term_id',
'terms' => $latest_edition)
))
);
Try it helped me and tell if it helped you or not
I have built a wordpress site that uses magic fields almost exclusively (rather than default posts etc).
However, I am now trying to implement search functionality and finding that wordpress is not able to find any content that is created by Magic Fields.
I have altered my search to create a custom WP_Query, but am still not having any luck. For example, I have a post_type of 'project':
$searchValue = $_GET['s'];
$args = array(
'post_type' => 'project',
'posts_per_page' => -1,
'meta_value' => $searchValue,
'meta_key' => 'title'
);
$query = new WP_Query($args);
This returns no results. Where am I going wrong?
Many thanks in advance!
I too have had problems with magic field and the wordpress search. The standard wordpress search only searches the post box content. The way to handle searching through magic field content is to search the postmeta.
$add_value = true;
$query_array = array();
$query_for_posts = "page_id=";
$search_guery = $_GET['s'];
$search_results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."postmeta WHERE meta_value LIKE '%" . $search_guery ."%' ORDER BY post_id");
if(!empty($search_results))
{
foreach ($search_results as $search_result)
{
//loop through results
for($i=0;$i<sizeof($query_array);$i++)
{
//check if post id in the array
if($search_result->post_id == $query_array[$i])
$add_value = false;
}
if($add_value)
{
//add the post id to the array if not a duplicate
array_push($query_array, $search_result->post_id);
//also add id for WP_Query
$query_for_posts .= $search_result->post_id . ",";
}
$add_value = true;
}
}
then to display the results.
if(!empty($query_array))
{
for($i=0;$i<sizeof($query_array);$i++)
{
//get post from array of ids
$post = get_page($query_array[$i]);
//make sure the post is published
if($post->post_status == 'publish')
echo '<h3>'.$post->post_title.'</h3>';
}
}
else
{
//tell the user there are no results
}
you can also use the $query_for_posts variable in a WP_query. It should have the value page_id=1,3,7,9,23... all the post ids from the search results.