I am trying to run two querys on a page.
The first shows the results of the custom user search
and the second is just supposed to display the more recent records with a few custom params hard coded into the query.
The problem i have is that when the custom search is run both queries are affected and the results are updated in both places.
I have tried placing these wp_reset_postdata();
wp_reset_query(); all over the place but all to no available.
Does anyone have any ideas?
This is my first query
$args = array('post_type' => 'vacancy', 'post_status' => 'publish' ) );
add_filter( 'posts_where', 'posts_where_title', 10, 2 );
function posts_where_title( $where, &$wp_query ) {
global $wpdb;
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%\'';
$where .= ' OR ' . $wpdb->posts . '.post_content LIKE \'%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%\')';
return $where;
}
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
//display results ?>
<?php $i++; endwhile; endif;
wp_reset_postdata();
wp_reset_query();?>
This is my second query...
$args = array( 'post_type' => 'vacancy', 'posts_per_page' => 10 );
query_posts( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//display results
endwhile;
wp_reset_postdata();
wp_reset_query();
Your problem is this line:
query_posts( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
query_posts affects the main loop, and just about every other loop that runs in that given page request. There are very few circumstances when it would be considered good practice to use it over WP_Query or even get_posts as you're affecting a LOT of the global variables Wordpress uses to populate information about what is being viewed.
Personally, I don't typically use the query method on WP_Query, but perhaps it might help in this scenario. Try changing your second query to something like this:
$args = array( 'post_type' => 'vacancy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
$loop->query( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
while ( $loop->have_posts() ) : $loop->the_post();
//display results
endwhile;
wp_reset_postdata();
wp_reset_query();
This localizes your query to a single instance of WP_Query, rather than affecting everything globally. It's untested, but let me know if this helps.
One more thing:
add_filter( 'posts_where', 'posts_where_title', 10, 2 );
function posts_where_title( $where, &$wp_query ) {
global $wpdb;
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%\'';
$where .= ' OR ' . $wpdb->posts . '.post_content LIKE \'%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%\')';
return $where;
}
Bear in mind that this filter will run on EVERY query you set after your call to add_filter. You might want to consider removing your filter at the point where more queries after it do not require the filter to run:
remove_filter( 'posts_where', 'posts_where_title');
Related
I have a custom taxonomy called mwp_ss_supp. I have a page that displays all the posts using this code:
<?php $count = wp_count_posts('mwp_ss_supp')->publish;
echo '<div id="post-count">' . $count . ' posts displayed</div>';?>
<?php
if ( have_posts() ) :
while (have_posts()) : the_post();
DO STUFF
<?php
endwhile;
endif; ?>
The problem is that all the posts are displaying in the order of latest post is the first on the list published. But I want to print them in a different order (ie, posts 1-10 are published first, posts 15-20 are published next, and 11-14 are published last, which ends up being published in alphabetical order by title). So how do I access this array of posts? There doesn't seem to be an array variable to play around with.
Show sql query for that. The sql query gets data from the database and determines the sort
Try adding a pre_get_posts() modifier to your theme's functions.php:
function taxo_posts($query)
{
if ($query->is_tax('mwp_ss_supp') && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' ); // or DESC
}
}
add_action('pre_get_posts', 'taxo_posts');
You need to build a custom wordpress query WP_Query() to query posts against taxonomies:
<?php
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_type',
);
);
'orderby'=> 'title',
'order' => 'ASC',
);
/**
* WP_Query()
* The WordPress Query class.
* #link https://developer.wordpress.org/reference/classes/wp_query/
*/
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
if( get_the_title() !== '' ):
the_title( '<h1>', '</h1>' );
endif;
if( get_the_content() !== '' ):
the_content();
endif;
endwhile;
endif;
/**
* wp_reset_postdata()
* After looping through a separate query, this function restores the $post global to the current post in the main query.
* #link https://developer.wordpress.org/reference/functions/wp_reset_postdata/
*/
wp_reset_postdata(); ?>
Learn more
wp_query # https://developer.wordpress.org/reference/classes/wp_query/
I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
I'm using a custom post type, and pulling that information throughout the site via a loop. It seems unnecessary to create a loop every time I want to call information from the custom post type. Is there another way?
Here's an example of what the loop looks like, and how I call it.
<?php
$args = 'post_type=post-custom&order=ASC';
query_posts($args);
if ( have_posts($args) ) : while ( have_posts() ) : the_post();
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
?>
<img src="<?php echo $my_meta['img'];?>">
<?php
endwhile;
endif;
?>
Any help is appreciated!
You need to use some kind of loop...If you don't want to use The Loop, you can use a standard foreach loop and get_posts(). Because you're only interested in getting post meta, you can use the fields argument to return an array of post IDs, instead of full post objects:
$args = array(
'post_type' => 'post-custom',
'order' => 'ASC',
'posts_per_page' => -1,
'fields' => 'ids'
);
$post_ids = get_posts( $args );
foreach ( $post_ids as $id ) {
$meta = get_post_meta( $id, '_my_meta', true );
echo '<img src="' . $meta['img'] . '">';
}
As a final note, you should basically never use query_posts().
I’ve been trying to do a WordPress query and have hit quite a major snag. Here’s the how I want to query the posts:
<?php query_posts( 's=#' . $user_login . '&author=-4,-5,-6&posts_per_page=25&paged='. $paged ); ?>
As you can see from this code, I’m attempting to exclude authors with the id’s of 4, 5, and 6. However, WordPress does not currently allow this functionality (as it does with categories).
Does anyone have any ideas how I can accomplish this goal—maybe a custom query/join? Any help would be much, much appreciated!
WordPress currently doesn't support removing multiple author posts from the query at a time.
But we can make use of another hook posts_where to remove the authors we do not need. But if we use this hook it will affect all the places in WordPress. So we should be careful when hooking this filter.
If you add it in the functions.php file it will run throughout all the queries which have post_where hook.
Add this function in your functions.php of the theme.
function remove_author_posts ($where) {
global $wpdb;
//add the author id's you need to remove
$removed_authors = array ('4', '5', '6');
$where .= ' AND ' . $wpdb->posts . '.post_author !=' . implode(' AND ' . $wpdb->posts . '.post_author !=', $removed_authors);
return $where;
}
Now add this filter to the place where you are calling the query_posts
add_filter ('posts_where', 'remove_author_posts');
Don't add this filter hook in your themes functions.php file. Only add in the place you need.
Now change your query posts and add the filter in the page you need like this:
query_posts( 's=' . $user_login . '&posts_per_page=25&paged='. $paged );
So the complete thing will look like
add_filter ('posts_where', 'remove_author_posts');
query_posts( 's=' . $user_login . '&posts_per_page=25&paged='. $paged );
UPDATE :
You can use global variable if you need to change the author id's dynamically. Add a new global variable $remove_authors before you add the filter.
global $removed_authors ;
//add the author id's you need to remove
$removed_authors = array ('4', '5', '6');
add_filter ('posts_where', 'remove_author_posts');
query_posts( 's=' . $user_login . '&posts_per_page=25&paged='. $paged );
Now change the remove_author_posts in functions.php file
function remove_author_posts ($where) {
global $wpdb, $removed_authors;
if (empty ($removed_authors) || !is_array($removed_authors))
return $where;
$where .= ' AND ' . $wpdb->posts . '.post_author !=' . implode(' AND ' . $wpdb->posts . '.post_author !=', $removed_authors);
return $where;
}
Hope this helps you :)
Change
<?php query_posts( 's=#' . $user_login . 'author=-4,-5,-6' '&posts_per_page=25' . '&paged='. $paged );
to
<?php query_posts( 's=' . $user_login . '&author=-4&author=-5&author=-6&posts_per_page=25&paged='. $paged );
To elimate multiple author call as
author=-4&author=-6 .... so on
Reference: http://www.yoursearchbuddy.com/wordpress-show-post-exclude-author
I know that this is an old post, but for the benefit of any googlers, you can now do this in a much nicer way with WP_Query, as of WordPress v3.7: http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
You can also exclude multiple authors this way:
$query = new WP_Query( array( 'author__not_in' => array( 4, 5, 6 ) ) );
I'm creating a wordpress widget to display the most commented articles over the last 4 days.
I have this so far
global $wpdb;
global $pagepost;
function filter_where( $where = '' ) {
// posts in the last 4 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-4 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( 'posts_per_page=4;orderby=comment_count' );
remove_filter( 'posts_where', 'filter_where' );
?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
echo "<li>";
echo "<a href='".get_permalink()."' class='linkTitle'>";
echo get_the_title();
echo "</a>";
woo_post_meta();
echo "<div class='fix'>";
echo "</li>";
endwhile;
echo "</ul>";
wp_reset_postdata();
Which from what I can find on the wordpress site should return the articles from the last 4 days ordered by comment_count
but it's just showing me my last 4 articles, I'm sure I'm doing something very obviously wrong here but I can't get it
I want the 4 articles with the most comments where the article was posted in the last 4 days.
someone please save what little hair I have left
Thanks to #swapnesh I found the answer.
My query wasn't correct, it should have been like this
$the_query = new WP_Query(array( 'posts_per_page' => 4, 'orderby' => 'comment_count', 'order'=> 'DESC' ));
$querystr = "
SELECT $wpdb->posts.* FROM $wpdb->posts WHERE 1=1 AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.comment_count DESC
";
Use this custom query
$pageposts = $wpdb->get_results($querystr, OBJECT);
echo''; print_r($pageposts); echo'';