Hide page on blog post - php

I need to query my pages to display on blog posts. But not all pages are going to be displayed so I've use metabox and create conditional checkbox if its going to be displayed or not.
My problem is when I query the post and pages, it doesn't show the posts/pages that are allowed to be shown on the blog page.
Assume that the meta_value is 1 which is set not to display and meta_value 0 is set display.
Below is my code:
$args = array(
'post_type' => array('post', 'page' ),
'meta_key' => 'gz_checkbox',
'meta_value' => '1',
'meta_compare' => '!='
);
query_posts($args);
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>

Related

How can I get have_posts() to look for posts in all languages?

We have a multilingual site with posts in many different languages. How can I display excerpts of all of an author's posts in all of the languages they have published in? have_posts() seems to only pull from the site's default language. Here's what I currently have in a custom author.php template:
<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
...
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'index' ); ?>
<?php endwhile; ?>
<?php else : ?>
<p><i><?php esc_html_e( 'This person has not authored any articles.' ); ?></i></p>
<?php endif; ?>
A var_dump for an author who only published in French, for example, returns FALSE.
I have also tried using get_posts(), but as you can see, this requires specifying the language, and I want to be able to pull posts in ANY language. The excerpting on this also isn't working correctly.
<?php
$args = array( 'post_type' => 'post', 'order' => 'DESC', 'author' => $curauth->ID, 'lang' => 'fr', 'post_status' => 'publish');
$posts = get_posts($args);
if (!empty($posts)) {
foreach($posts as $post => $post_val){ ?>
<h1 class="entry-title textcenter-xs"><?php echo $post_val->post_title ?></h1>
<?php
echo wp_trim_excerpt($post_val->post_content); //This is returning ALL the content, not the excerpt
}
}
?>
I'm still new to php, so any help appreciated!
by removing the 'lang' => 'fr' parameter you should get a loop that display all post available. gl.

Having a trouble loop for specific taxonomy

Before, sorry for my bad grammar/English and bad code understanding
I'm testing a new website about short or series story with custom post type generated by Writeshare plugin. What I would like to achieve on my dynamic page is like this
Custom term (term name: Short Stories | slug: short term | tag ID: 2) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
Custom term (term name: Series | slug: series | tag ID: 3) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
My website is: https://qisa.xyz/ | and I'm using justread theme https://wordpress.org/themes/justread/
but I always failed when try to adding the tax to the loop, while custom post type (called WPWS content) successfully added to the home page via pre_get_post in functions.php
I've been trying modified the code trying to only get post from needed taxonomy in pre_get_post function and loop but nothing worked so far.
Now my code is consisting default first loop (all content from custom post type, worked perfectly) and second loop (modified to specific taxonomy, as I write this thread in 'nothing found' status
And what the best approach to achieve this. Adding new WP_query? or through pre_get_post function?
code in function.php
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'wpws_content' ) );
return $query;
}
now code in index.php
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php the_posts_pagination(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main grid grid--4">
<?php $temp_query = clone $wp_query; ?>
<?php query_posts( 'taxonomy_name=short' ); ?>
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( $temp_query->have_posts() ) : $temp_query->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
Update:
I tried implementing the solution from Manoj Webvertex, but that was sent me into technical error. Where did the code go wrong?
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$format&term=$short->slug&posts_per_page=5" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
<?php wp_reset_query(); ?>'
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
//Do you general query loop here
endwhile;
endif;
endforeach;
endforeach;

Random order in WordPress Loop?

I'm using the basic loop code in a taxonomy archive (artists) and I was wondering how you can set the loop to show posts in random order ('orderby'=>'rand') it doesn't seem to work when I add the array? Any help would be great!
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
array ( 'orderby' => 'RAND' );
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
more info for query
query_posts(array(
'showposts' => 6,
'orderby' => 'rand',
'category_name' => 'News' //You can insert any category name
));
Nice Question first !
You can do that with simple using function of PHP.
http://www.php.net/manual/en/function.shuffle.php
Follow below step:
First thing is get all post with query
You know that wordpress will provide the result in array format. So, do not try more coding its too complecated.
Now you have array of result so just use PHP function shuffle.
http://www.php.net/manual/en/function.shuffle.php
Please ask me after implementation if any query.
Thanks !
Try this:
<?php
$args = array(
'orderby' => 'rand'
);
$query = new WP_Query($args);
if (have_posts()) {
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
You have two ways of doing it. The first way is not the best way, but it may be simpler for you to understand:
Using WP_Query
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
Here, we'll be using a custom WP_Query object and orderby to get random posts.
Using pre_get_posts
The best way to do it is by using the pre_get_post action to modify the page output automatically. You might need some more coding though.

wordpress query_posts alternative

I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!

Creating a separate wordpress home page and blog

Hello I want to Create a home page that has updating blog entries.
So 4 lists of headlines from different categories
And I want to have a link to the regular blog page with a different template.
Right now I just changed index.php around to have the containers for the featured posts content.
So this is a two part question how do I get these mini updates for the thing
I want to use query_posts() multiple times I assume and separate by category.
And how do I make a linkable page to a blog.php file which currently is telling me that all these functions are undefined.
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
If you want to create a page that includes WP data/posts outside the themes or blog folder, you need to include and make available the Wordpress functions first:
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
And then you can make the queries for each set of posts by category:
$args = array( 'numberposts' => '5, 'offset'=> 1, 'category' => 'your category ID' );
$myposts = get_posts( $args );
foreach($myposts as $post) {
...some code...
}
I hope it helps.

Categories