I want to only show posts on a wordpress page that belong to logged in users AND contain a custom meta key "color." I have the code for each option i.e. I can either show posts belonging to logged in users or posts with a meta key = color but I cant work out how to combine both so both conditions need to be true. Below are the 2 pieces of code i'm working with:
<!-- If Logged In -->
<?php
if ( is_user_logged_in() ):
global $current_user;
get_currentuserinfo();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts() : $author_posts->the_post();
get_template_part( 'content', 'page', 'the_meta()' );
?>
<!-- If Posts Contains Color Meta -->
<?php
$the_query = new WP_Query('meta_key=color');
while ($the_query->have_posts() ) : $the_query->the_post();
endwhile;
?>
Does anyone know how to combine them so posts that match both conditions are shown?
Something like this should work:
<?php
if ( is_user_logged_in() ) :
global $current_user;
get_currentuserinfo();
$query = array('posts_per_page' => '-1','author' => $current_user->ID, 'meta_key' => 'color');
$result = new WP_Query($query);
while ($result->have_posts()) : $result->the_post();
get_template_part( 'content', 'page', 'the_meta()' );
endwhile;
endif;
?>
Related
I've been trying to formulate a way to query by a user role type in wordpress and have been using the following code which I found on Stack Overflow but it isn't working for me:
<?php
$friends = get_users( array( 'role' => 'mns' ) );
$friend_ids = array();
foreach( $friends as $friend )
$friend_ids[] = $friend->ID;
$news = new WP_Query( array( 'author' => implode( ',', $friend_ids ), 'post_type' => 'roleplays', ) );
?>
<?php if ( $news->have_posts() ) : while ( $news->have_posts() ) : $news->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
The loop collects all the posts in the custom post type, but doesn't show only posts from the specific role type.
Any ideas?
Thanks in advance.
Josh
Do you included "wp_reset_postdata();"? When using the_post(), wp_reset_postdata() should be use to ensure that you don't get unexpected outcome. Outside that, I don't see what is causing the problem. Is there other custom php codes or plugins that may be interfering?
I'm trying to create a set of WP pages with indices to all posts which have tag X as well as category Y.
I found this which seems to do exactly what I want and I understand how to edit it for each index page, but I don't know where/how to use the actual code to create the index pages.
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
TIA for your help.
This is what finally worked - note that I needed the category ID but the tag slug.
<?php if ( have_posts() ) : ?>
<?php query_posts( 'cat=6&tag=a1&&orderby=title&order=asc' );?>
<?php while ( have_posts() ) : the_post();?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
You use that code to replace the basic query in the loop in your template.
https://codex.wordpress.org/Function_Reference/query_posts
I am trying to modify my tag.php inside Wordpress. Basically, I have my general loop set to the default five posts per page. When users click on a tag from my tag cloud, I'd like it to display all of the relevant results by title. Here is what I have in tag.php:
<p>Tag: <?php single_tag_title(); ?></p>
<?php if (have_posts()) : while (have_posts ()) : the_post (); ?>
<p><?php the_title(); ?></p>
<?php endwhile; endif; ?>
This works perfectly, however it only returns the default amount of five. When I try to add a wp_query using ('posts_per_page' => 1000) before the loop, it returns all of my sites posts, rather than just for the appropriate tag. How can add more results? Thanks!
Thank you very much for the reply SilverKenn, I appreciate it. I was able to sort it out using this instead.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
instead of editing a php file whenever you try to customize something via wordpress, you can modify almost everything using functions.php depending on how you code the theme,
Check out post_limits filter http://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits
or pre_get_posts http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
you can limit the results using the above filters,
e.g. pre_get_posts
function cmk_custom_result( $wp_query ) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'your-post-type' && is_tag() ) {
$wp_query->set('posts_per_page', '25');
}
}
add_filter('pre_get_posts', 'cmk_custom_result');
post_limist
function cmk_post_result_limits( $limit, $query ) {
if ( !is_admin() && $query->is_main_query() && is_tag() ) {
return 'LIMIT 0, 25';
}
return $limit;
}
add_filter( 'post_limits', 'cmk_post_result_limits', 10, 2 );
Use get_query_var to get the correct tag, like: 'tag'=> get_query_var('tag') then use -1 in your posts array for 'posts_per_page' to get an unlimited number of posts for that tag, otherwise change the number to whatever you want to limit the output.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
I have a code like this in my index ,archives etc..
<?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('showposts=16&paged='.$page_num); ?>
<?php if ( have_posts() ) : ?>
My Option
get_cat_id(get_option('mtn_blogcategory')
But I have a custom blog template and I want to display blog posts only on blog template how can I exlude category using this option above from a loop like index,archive,search etc..?
Thanks
thanks for your response I tried like this
<?php
$bcat = get_option('mtn_blogcat');
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('showposts=16&paged='.$page_num.'&cat=-'.$bcat); ?>
<?php if ( have_posts() ) : ?>
Did'nt work :(
A few things first:
The function query_posts() is used to alter the main query. It isn't meant to be used by plugins or themes. So unless you have a very good reason not to, use WP_Query instead:
$the_query = new WP_Query( $args );
It seems as if you're trying to create a query that gets all posts except those that are in the category with slug mtn_blogcat. You're also trying to make sure it's on the correct page and that it shows 16 posts per page.
The parameters needed to achieve this are cat, paged and posts_per_page.
Assuming you don't already know the ID of the category you wish to exclude, you first need to get it. You can get the ID either by the category's slug or by its name.
If you know the category's name:
$cat_ID = get_cat_ID( 'Category Name' );
If you know the category's slug:
$category = get_category_by_slug( 'category-slug' );
$cat_ID = $category->term_id;
You should only need to use one of those function. It seems the slug might be mtn_blogcat(?), if that's the case use get_category_by_slug( 'mtn_blogcat' );
Next, we're getting the current page number:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
And lastly we set the posts_per_page to 16:
$posts_per_page = 16;
Putting it together:
<?php
// Assuming you know the category slug
$category = get_category_by_slug( 'category-slug' );
$cat_ID = $category->term_id;
$exclude_cat = '-' . $cat_ID;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts_per_page = 16;
$args = array(
'cat' => $exclude_cat,
'paged' => $paged,
'posts_per_page' => $posts_per_page
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Put posts markup here -->
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The only thing left is to put the existing post markup where I've put the comment:
<!-- Put posts markup here -->
i have next loop:
<?php woocommerce_product_loop_start(); ?>
<?php
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
From some reason that's display always the same 4 products. what i want is - that on every refresh it will display differents products from all categories i have on WooCommerce.
What i need to add to the loop, or should i need to create something else for that?
it might be a conflict with plugin. Some plugins disable ability for random ordering unless you filter it out with: remove_all_filters('posts_orderby');.
So try to put that before your query:
<?php
remove_all_filters('posts_orderby');
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );