Wordpress get post id with matched post content - wordpress

Is there a way in Wordpress so that I would generate a sub-string of a post content and it would give me the post id?
Thanks in advance.

It can be achieved using awesome WP Query Class of WordPress with search parameter. e.g. Following query will search for any posts with "hello" words in it and display its Post Ids as you needed:
<?php
// 's' parameter is for search
$the_query = new WP_Query( array( 's' => 'hello' ) );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//now we can echo anything we want like e.g. IDs
echo get_the_ID();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Related

I want to create a Wordpress archive page that dispays posts with tags matching query variable. Are there any plugins available to help me do this?

Thanks for your reading time. I want to create a nice looking archive page that displays posts with tags matching the public query var.
E.G. www.example.com/?tag=london. Should display the posts with tag london.
I would like to display posts: Featured image, excerpt en read more and want to order by a value in a custom field.
Can anyone point me a plugin or other suggestion to help me out? I am new to wordpress/coding and hope you can help me a bit, thanks Martin
Got reference from HERE
/List of tag slugs
$tags = array('foo', 'bar', 'chocolate', 'mango', 'hammock', 'leaf');
$args = array(
'tag_slug__in' => $tags
//Add other arguments here
);
// This query contains posts with at least one matching tag
$tagged_posts = new WP_Query($args);
echo '<ul>';
while ( $tagged_posts->have_posts() ) : $tagged_posts->the_post();
// Check each single post for up to 3 matching tags and output <li>
$tag_count = 0;
$tag_min_match = 3;
foreach ( $tags as $tag ) {
if ( has_tag( $tag ) && $tag_count < $tag_min_match ) {
$tag_count ++;
}
}
if ($tag_count == $tag_min_match) {
//Echo list style here
echo '<li>'. get_the_title() .'</li>';
}
endwhile;
wp_reset_query();
echo '</ul>';

Wordpress standard loop: additional post count with parameters

I am altering a template file of the Search & Filter Wordpress Plugin to show additional post counts.
The plugin uses the standard Wordpress loop to query posts by certain parameters. The number of posts can be counted by using found_posts.
Now I want to display a second post count that takes additional parameters into account, eg. post_status. I have to stick to the regular WP loop to keep the query from the Plugin intact.
Something like this in the commented line:
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
The code works fine, except for the commented part which obviously doesn't work.
Is there a way to add another post_count with additional parameters to the standard loop?
Thanks
Georg
You could not add like this echo $query->found_posts(array('post_status=>'private') but before your loop just use below code to count posts by status. in your case you want to count posts by status private so add below code.
$args = array('post_type' => 'your_post_type_name','post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash')
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
$count_posts = wp_count_posts('post');
$private_posts = $count_posts->private;
echo $private_posts. 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
similarly if you want to show count for publish or draft posts you can add below code.
$publish_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
you could count any other posts by its status is well.

Getting Only Custom Post Types Posts with WP_Query

I am trying to get custom posts with WP_Query but it's not returning only custom post type posts but also default posts too.
I am using
$args = array (
'post_type' => array ('survey')
);
$sPosts = new WP_Query($args);
as a result I am getting 'survey' posts as well as default posts, I only need it to return 'survey' posts.
P.S. when I use query_posts() it returns the required result, but just not getting it done with WP_Query and I prefer to use WP_Query not the query_posts()
Please try it like that....
<?php
$new = new WP_Query('post_type=discography');
while ($new->have_posts()) : $new->the_post();
the_content();
endwhile;
?>
I believe this custom query should actually be your main query, in which case you should not use a custom query
The problem you are facing is either due to
Wrong use somewhere of pre_get_posts. Remember, pre_get_posts alters all instances of WP_Query, backend and front end. Lack of correct use will break all queries
You have not changed the loop to be objects of your new query
To come back to the point, as said, this is suppose to be the main query, so lets target that problem.
The first thing to do would be to delete the custom query. Once you have done this, you would only see normal post.
We are now going to use pre_get_posts to alter the main query to only show custom posts. Paste the following inside your functions.php
add_action( 'pre_get_posts', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_home() ) {
$q->set( 'post_type', 'YOUR CPT' );
}
});
You should now just see post from your cpt on the homepage
EDIT
Your index.php should look something like this
if( have_posts() ) {
while( have_posts() ) {
the_post();
// HTML mark up and template tags like the_title()
}
}else{
echo 'No posts found';
}
This code can get all the posts in a custom post type..,
wp_query is the function can get all the posts in the custom post type. wp_query requires array, so you can give the custom post type name in post id to array
$args = array(
'post_type' => 'address', //custom post type
'posts_per_page' => -1 // get all posts
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
echo( get_the_title() );
}
}

Search using custom fields in wordpress

I am busy developing a web application using WordPress. I have created a custom post with a few custom fields. When I search for post using WordPress search box only post with title that match the search string get returned. I want to add custom fields on searching domain.
I there a to search by custom field values in WordPress?
following can do
$args=array(
'post_type'=>'custom post',
'order'=>'ASC',
'orderby'=>'menu_order',
'meta_query' => array (
array (
'key' => 'meta-key',
'value' => 'meta-value',
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo the_title();
}
}
wp_reset_postdata();
To query a bunch of custom fields I found it easier to use the search filters instead.
http://codex.wordpress.org/Custom_Queries Scroll down to the "Keyword Search in Plugin Table" section for an example.
Here's a quick snippet of code form my custom 'posts_where' filter so you can get an idea:
function custom_search_where($where) {
// put the custom fields into an array
$customs = array('custom_field1', 'custom_field2', 'custom_field3');
foreach($customs as $custom) {
$query .= " OR (";
$query .= "(m.meta_key = '$custom')";
$query .= " AND (m.meta_value LIKE '{$n}{$term}{$n}')";
$query .= ")";
}
$where = " AND ({$query}) AND ($wpdb->posts.post_status = 'publish') ";
return($where);
}
add_filter('posts_where', 'custom_search_where');
There's a lot more code but between the Codex example and the snippet above, it should give you a good idea.

wordpress group by post type on search page

I would like to display search results grouped by post type. I have regular posts, pages,
and a custom post type of product. How would I accomplish this by editing the below code.
The code below just shows all posts and pages right now.
<?php
while (have_posts()) : the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
?>
This code alters the original search query to order the results by post-type in the order you select. There are other solutions, but this is the only one i found that doesn't break pagination or requires multiple queries.
add_filter('posts_orderby', 'my_custom_orderby', 10, 2);
function my_custom_orderby($orderby_statement, $object) {
global $wpdb;
if (!is_search())
return $orderby_statement;
// Disable this filter for future queries (only use this filter for the main query in a search page)
remove_filter(current_filter(), __FUNCTION__);
$orderby_statement = "FIELD(".$wpdb - > prefix.
"posts.post_type, 'post-type-c', 'post-type-example-a', 'custom-post-type-b') ASC";
return $orderby_statement;
}
In your case, I'd do two things:
filter the search page's initial query to a particular post type
use one WP_Query call for each remaining post type
For (1), this would go in your functions.php:
<?php
function SearchFilter($query) {
if ($query->is_search && !is_admin()) {
if (isset($query->query["post_type"])) {
$query->set('post_type', $query->query["post_type"]);
} else {
$query->set('post_type', 'product');
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
For (2), adapt the code you provided from your template file:
<?php
$s = isset($_GET["s"]) ? $_GET["s"] : "";
$posts = new WP_Query("s=$s&post_type=post");
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
wp_reset_postdata();
endif;
?>
You can re-use this code for each other post type.
It's best to avoid using query_posts... see querying posts without query_posts (even WordPress devs agree).
You need to alter the post query to reorder things. You would execute this just before you enter the loop. You can read more about query_posts in the Wordpress codex.
http://codex.wordpress.org/Function_Reference/query_posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('type1', 'type2') ) );
query_posts( $args );
//the loop

Resources