I have made a custom field "checkbox". I want to exclude posts only that have the checkbox checked.
I am not able to do so.
Here is the code that i am using
$args = array(
'posts_per_page'=> -1,
'post_type' => 'latestnews',
'orderby'=> 'id',
'meta_key'=>'',
'meta_value'=>'',
'order'=> 'asc',
);
query_posts($args);
$my_posts = new WP_Query($args);
So basically i want the posts only that doesn't have the meta key "sticky" set and meta value does not exists.
here my meta key is a checkbox and the value yes is stored in the database if i check the checkbox and nothing is stored in the database if i do not check the option.
<?php
$check_metakey = get_post_meta($post->ID, "sticky", true);
if($check_metakey !== '') :
?>
your article's template
<?php
endif;
?>
NOTE: All this INSIDE the while cycle.
Good luck! :)
Related
I simply want to add a field to all my products on woocommerce which isn't visible to customers.
This will be useful when exporting the product list to csv and calculating based on this value later.
Probably overlooked something obvious.
Thanks in advance.
First, you need to get all of your products. After that, check this meta key is already exists. If yes, then update the value; otherwise, add it as a new meta key.
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
if(!empty(get_post_meta($post->ID, '_your_custom_meta_key', true))){
update_post_meta($post->ID, '_your_custom_meta_key', 'Your custom value');
}else{
add_post_meta($post->ID, '_your_custom_meta_key', 'Your custom value');
}
endwhile;
wp_reset_query();
?>
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.
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']
)
);
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,
)
);
Is there a way to programmatically trigger the standard displaying post list from a custom WP_Query, just like a "category" menu item does?
To clarify:
i'm not looking for plugins like List category posts that do the work but with a custom built-in template..
I need the wp's(theme's) standard post list rendering loop to be triggered !
Thanks!
Put this code where ever you need to render list of post from category "example_category_slug", (or any other taxonomy).
$wpq = array ('taxonomy'=>'category','term'=>'example_category_slug');
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<ul>";
if ($article_count){
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
}
echo "</ul>";
or use simpler query, like $myquery = new WP_Query ('cat= 11, 9'); for cat ids, or any other wp_query
But while choosing where to put it, consider Template Hierarchy as a set of rules which template file is right to use.
If you need a list of categoires insted post use wp_list_categories
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list'
);
wp_list_categories($args);
?>
ful ref. in wp codex
Here is a way to do it..
inside a shortcode's function, or in a function that act ajax style, put a code like that:
global $wp_query;
$cat = $_GET['categoria'];
$wp_query->init();
$wp_query->query(array(
'posts_per_page' => 4,
'category_name' => $cat
));
get_template_part( 'blog', 'columns' );
wp_reset_query();
die();
the get_template_part arguments are theme dependent and can be desumed from php template names from theme's root directory.
in this case wp will call [theme's_dir]/blog-columns.php..
i reccomend the lecture #Michal reccomended me