Custom post query breaks original search term in Wordpress loop - wordpress

I want to create a loop and query posts by their author role and display the results of a search term based on the authors role.
When I apply my query the posts returned are for the user role and not the original search term.
Here's my query and the loop;
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
This is on my search.php page ...
So for example if I have two posts published from the user type 'administrator' one of those posts is tagged with 'England' and the other is not - so when I go to the search form and enter 'England' I would expect this code to display the one post out of those two however it doesn't, it displays both.
So it appears to be just displaying all posts from the user type 'administrator' and ignoring the search term.
I've got no idea why that is the case, in my mind this code should be working so if anyone has any advice or can point me in the right direction that would be great!

You are creating a custom loop so you need to add the search term back into the query args.
$query = new WP_Query(
array(
'author__in' => $ids,
's' => $_GET['s']
)
);

Related

Wordpress how to query multiple custom post types

I'm using custom post types UI plugin on my website.
Now I have blog posts ( default wp posts - news ) and a new custom post type ( articles ).
I want to show all the posts for these two post types on another page.
but I can not combine this two together!
Here is what i'm using to show articles posts:
public function News() {
$terms = get_terms(['post_type' => 'articles','taxonomy' => "article_category",'hide_empty' => false,]);
This could easily be done by using wp_query. Please avoid writing your own sql queries as much as possible because these custom sql queries would potentially make you vulnerable to slq attacks. You could combine your post types like this 'post_type' => array( 'post', 'articles' ).
$args = array(
'post_type' => array( 'post', 'articles' ),
'posts_per_page' => -1
);
$custom_query = new WP_Query( $args );
if ($custom_query) {
while ($custom_query->have_posts()) {
$custom_query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
<?php
}
}
wp_reset_postdata();
Hi if WP Query does not work for you, you can try and get the id's of these two types. Then make a function that pulls the information that you need from each one based on their id's.
global $wpdb;
$ids = $wpdb->get_results(" SELECT `ID` FROM `wp_posts` WHERE `post_type` LIKE 'post' OR `post_type` LIKE 'articles' ";
foreach($ids as $id){
//use another custom function to get the ACF or postmeta whatever it is you need
}

Filtering Products of same price in one page wordpress

So i have an online store with variations of products but ill like to have a page that shows products with a price range. Say I have a page called 3k Store. I want such that when a user goes to that page, it filters and displays only goods that cost 3000, Please how do i go about this? I am using wordpress and woocommerce. Thanks.
1. Create a page template to list products
Create a page template to be able to put some code on it. Name it page.list-product-in-range.php for example and add this code at the start of the file to make it a template :
<?php // Template Name: List product in range ?>
<?php get_header(); ?>
<?php get_footer(); ?>
2. Create a page in the wordpress admin
Create a page named Product that cost 3000for example and select the template previously created. That's all for the moment.
3. Go to the php file that we created in part 1
Between <?php get_header(); ?> and <?php get_footer(); ?>, add this part to get the products that cost 3000 :
( I let the range in the meta_query so you can easily set something else than 3000 by playing with the number inside value)
<?php
$products = get_posts(
array(
'posts_per_page' => -1,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => array(
3000 ,
3000
),
'compare' => 'BETWEEN',
'type'=> 'NUMERIC'
)
)
)
);
?>
4. Add the loop to list products
After the code that we added in part 3 put this code :
<ul class="products">
<?php
if (!empty($products)) {
foreach ( $products as $post ) : setup_postdata($post);
wc_get_template_part( 'content', 'product' );
endforeach;
} else {
echo __( 'No products found', 'woocommerce' );
}
wp_reset_postdata();
?>
</ul>
You also can use pre_get_posts hook to set the products in the main query or instead of get_posts() use the WP_Query class. I only use get_posts() for the simplicity .
If you need pagination (and i think you will need), you should think about using pre_get_posts or WP_Query.
Code tested and it works.

WordPress wp_query + filter result (no custom filed) + next_post_link()

I did an extensive search but could not find the answer (perhaps I'm not using the right search terms?).
Anyway, here's what I'm trying to accomplish:
I'm using wp_query to return user submitted posts that have been published. I am showing one post at a time and using next_posts_link() to let the user to advance to the next post. This is all working fine. I'm also using the wp-postratings plugin to the user to rate these user submissions. I only want to show posts that the user has not already rated. For that, I'm using:
check_rated_username($post->ID)
This part is actually also working. So far my code looks like this:
$my_query = new WP_Query( array (
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
));
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$rated = check_rated_username($post->ID);
if (!$rated) : ?>
<?php //the_title() etc. Show post details (it's long so I'm not going to post the whole thing, but the post details are showing up fine) ?>
<?php next_posts_link('Next →', 10); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif; ?>
The problem next_posts_link() retrieves posts that meet the parameters set in the wp_query (and therefore are not part of the "if (!rated)" statement. There are instances when I click on "Next", and it shows a blank page--a post that this particular user has already rated.
How can I set it up so that I'm showing one post that the user has not rated on each page AND allow the user to navigate to the next unrated post by clicking on a NEXT button? I'm not married to the approach I have come up with. I just need to achieve this end result.
Thanks!
What you can do is first to fetch all posts id:s that a user has rate, then you can add an post__not_in in your query. It can look something like this:
<?php
/* Get all posts id:s that current user has rated */
$user_ratings = $wpdb->get_results( $wpdb->prepare( "SELECT rating_postid FROM {$wpdb->ratings} WHERE rating_userid = %d", get_current_user_id() ) ); ?>
<?php
/*Set up an empty array for save id:s in */
$post_not_in = array();
/* Check if $userRating has any values, if so loop thru and put the id into the array */
if($user_ratings){
foreach($user_ratings as $user_rating){
$post_not_in[] = $user_rating->rating_postid;
}
}
?>
<?php
/* The loop */
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'post__not_in' => $post_not_in
);
$my_query = new WP_Query($args);?>
The rest of your code
This way the loop only have posts that the current user haven't rated yet.

How to hook in a custom query to the loop with Wordpress

I want to create a loop and query posts by their author role. And display the results of a search term based on the authors role.
I've tried creating a function to alter the query's where clause:
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
I'm trying to add a WP_Query to the loop but this is where I get stuck with the results not getting filtered by role so I'm fairly certain I must be implementing this wrong - this is the first time I've tried to do something like this so sorry if it's a dump question but I can't find an answer to my question so if anyone can point me in the right direction that would be amazing!
Any advice welcome, thank you!
You can try the posts_where
hook.
UPDATE:
Why don't you use the default WP_Query author arg instead hook.
You query will be :
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

Resources