I thought this question would be an easy find on the internet… however, it seems no one has ever ran into this issue before.
There's no problem in listing a search query when you enter your search term, I needed posts and pages to be differentiated in a way. For example: I'd like results that are posts (articles) to display the author, read time, and post date, but I do not like these metas to appear on results that are pages.
Here's a code:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$searchArgs = array(
's' => $s,
'paged' => $paged,
'showposts' => -1,
);
$searchQuery = new WP_Query( $searchArgs );
if ( $searchQuery->have_posts() ) :
echo '<h2>';
echo 'result: '.search_query();
echo '</h2>';
while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
echo 'link: '.get_the_permalink();
echo 'Title: '.get_the_title();
echo 'Post Summary: '.get_the_excerpt();
$how = ( is_single() ) ? 'this result is a post' : 'your method did not work'; //always return false
$how = ( is_page() ) ? 'this result is a page' : 'your method did not work'; //always return false.
echo 'Post Type: '.$how; // <------ How to do?
//***Problem***: is_single() or is_page() does not work, by the way (both return false).
//I've crossed-checked that the results are actually either a post or a page.
endwhile;
endif;
if ( function_exists('custom_pagination') ) {
custom_pagination($searchQuery->max_num_pages,"",$paged);
}
So my problem is, is_page() and is_single() does not work within the Search Query I made.
How do you do this the right way?
I thought this question would be an easy find on the internet… however, it seems no one has ever ran into this issue before.
There's no problem in listing a search query when you enter your search term, I needed posts and pages to be differentiated in a way. For example: I'd like results that are posts (articles) to display the author, read time, and post date, but I do not like these metas to appear on results that are pages.
Here's a code:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$searchArgs = array(
's' => $s,
'paged' => $paged,
'showposts' => -1,
);
$searchQuery = new WP_Query( $searchArgs );
if ( $searchQuery->have_posts() ) :
echo '<h2>';
echo 'result: '.search_query();
echo '</h2>';
while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
echo 'link: '.get_the_permalink();
echo 'Title: '.get_the_title();
echo 'Post Summary: '.get_the_excerpt();
$how = ( is_single() ) ? 'this result is a post' : 'your method did not work'; //always return false
$how = ( is_page() ) ? 'this result is a page' : 'your method did not work'; //always return false.
echo 'Post Type: '.$how; // <------ How to do?
//***Problem***: is_single() or is_page() does not work, by the way (both return false).
//I've crossed-checked that the results are actually either a post or a page.
endwhile;
endif;
if ( function_exists('custom_pagination') ) {
custom_pagination($searchQuery->max_num_pages,"",$paged);
}
So my problem is, is_page() and is_single() does not work within the Search Query I made.
How do you do this the right way?
Share Improve this question edited Nov 13, 2018 at 17:52 vm7488 asked Nov 13, 2018 at 17:45 vm7488vm7488 331 silver badge5 bronze badges2 Answers
Reset to default 1That will only tell you if it's a Post or a Page. What if it's a category, tag, archive, or custom post type? Here's how I'd write that, if you care.
function xyz_get_post_type_name() {
$wp_type = get_post_type( get_the_ID() );
switch ($wp_type) {
case 'post' :
$type_name = 'Article';
break;
case 'page' :
$type_name = 'Web Page';
break;
case 'quote' :
$type_name = 'Testimonial';
break;
case 'post_tag' :
$type_name = 'Topic';
break;
default :
$type_name = ucfirst($wp_type);
break;
} // END switch
return $type_name;
} // END xyz_get_post_type_name()
Then just echo that function inside your loop wherever you want it.
while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
echo 'Post Type Name: ' . xyz_get_post_type_name();
endwhile;
The switch statement would also allow you to give these post types a "pretty" name and default to just their actual name (first letter capitalized).
…Figured it out… 5 minutes later…
$queryIsPost = (get_post_type() === 'post') ? true : false; //the solution
$queryIsPage = (get_post_type() === 'page') ? true : false; //the solution
$searchArgs = array(
's' => $s,
'paged' => $paged,
'showposts' => -1,
);
$searchQuery = new WP_Query( $searchArgs );
if ( $searchQuery->have_posts() ) :
echo '<h2>';
echo 'result: '.search_query();
echo '</h2>';
while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
echo 'link: '.get_the_permalink();
echo 'Title: '.get_the_title();
echo 'Post Summary: '.get_the_excerpt();
if ($queryIsPost) {
$thePostType = "Post";
} elseif ($queryIsPage) {
$thePostType = "Page";
} else {
$thePostType = "Error. Not determined";
}
echo 'Post Type: '.$thePostType;
endwhile;
endif;