get_posts on a WP static page - php

This is my first time asking a question on this forum. I hope i can be specific enough. I am trying to replace the front page posts section of a WP template. I have the code below running fine on my index.php page (it gets the posts) when the WP theme is set to settings>reading>your latest posts BUT the way the WP theme "the thinker" is setup reading is set to static page and the posts are gotten through a blog template page (in same location as the index.php file) which gets the posts from loops in separately included files. I'd like to keep it set to static for a few reasons. My question is "Is there a reason why the code below would only work in an index.php file and not out of a blog-template file which is is in the same location as the index.php file. I've checked and the the template-parts called in the code are being called. It seems like there's just no posts to get (which there are).
Thank you for your time,
Dave
<!-- blog content -->
<div class="container">
<div class="row" id="primary">
<main id="content" class="col-sm-8" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- content -->
<!-- sidebar -->
<aside class="col-sm-4">
<?php get_sidebar(); ?>
</aside>
</div><!-- primary -->
</div><!-- container -->

have_posts() functions only allowed you to check whether the page have the post or not and as it is your static pages, there will be no posts assign to this page.
You need to query first at the start of the page to show posts. Here is the example.
EDITED::
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
You can find more about WP_Query in below link.

Related

WP Theme build in Search engine shows post and products instead only products

I am working on my shop and I ran into a problem that I can't deal with because I miss php knowledge (still learning). The built-in search engine shows lists of posts and products in the results. It doesn't look good, I just want the products and thumbnail view as in the categories. I would prefer not to change the search engine to a different one. Please let me know if changing the code to search.php will allow me to achieve this effect? If so, which elements should be changed to what?
Search.php code:
<?php if ( have_posts() ) : global $wp_query; ?>
<header>
<h1 class="selleradise_page__title">
<?php
printf(
/* translators: %s: Search Term. */
esc_html( __("Search Results for: %s", "selleradise-lite") ),
'<em>' . get_search_query() . '</em>'
);
?>
</h1>
</header><!-- .page-header -->
<main id="main" class="site-main" role="main">
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content/search');
endwhile;
if($wp_query->max_num_pages > 1): ?>
<div class="selleradise_pagination">
<?php
$args = [
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'array',
'end_size' => 0,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => selleradise_svg('unicons-line/angle-left-b'),
'next_text' => selleradise_svg('unicons-line/angle-right-b'),
'add_args' => false,
'add_fragment' => '',
'aria_current' => "page",
];
echo paginate_links($args);
?>
</div>
<?php endif;
else :
get_template_part( 'template-parts/content/none');
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
If you can find where the WP theme sets the post_type to both post and product then that would be better, it could be on the functions.php file or something.
If not, then you can try to manipulate the WP Query on the search.php file, something like this:
<?php
// Preserve original wp_query
global $wp_query;
$original_query = $wp_query;
// Assign new query to include only products
$wp_query = null;
$args = array('post_type' => 'product');
$wp_query = new WP_Query( $args );
if ( have_posts() ) : ?>
<header>
...
</header>
<main id="main" class="site-main" role="main">
<?php
// Start the Loop
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content/search');
endwhile;
if($wp_query->max_num_pages > 1): ?>
...
<?php endif;
else :
get_template_part( 'template-parts/content/none');
endif;
// Reset post query
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata(); ?>
</main>
Refer to these for WP_Query and for resetting postdata.

Sidebar is appearing on custom post pages without being called

I'm creating a Wordpress theme for a graphic portfolio website, and I'm utilizing the sidebar.php for a hero image on the index page that is not meant to appear on other pages. While this works for all static inside pages, I have two custom post categories, and the sidebar section has started appearing above those posts.
I should note that this hasn't always been a problem, and started cropping up for no apparent reason while working on something else.
On index.php, the sidebar is called as follows:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; ?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
<?php endif;?>
<?php get_footer(); ?>
While page.php is similar but lacks the php line calling the sidebar, as follows:
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
page-fine-art.php also lacks that php line, as follows:
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'fine-art',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$custom_query = new WP_Query( $args );
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="blog-post">
<h2 class="blog-post-title"><? php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?> by <?php the_author(); ?></p>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
The function creating the the custom posts for "fine art" goes:
function create_my_custom_posts() {
register_post_type( 'fine-art',
array(
'labels' => array(
'name' => __( 'Fine Art' ),
'singular_name' => __( 'Fine Art' ),
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
)
));
I don't see where sidebar.php is being called in the final example, or where else it could be coming from.

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

Iterating over correct number of wordpress posts from category + custom post type

Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.

Pagination with Wordpress using "wp-query"

Hey all i'm running a query loop in a custom page.php using a custom post type. I would for it to paginated so i could have a specific amount of post per page. I was wondering if someone could help me out. I have added my code below:
<?php query_posts( array(
'post_type' => array( 'POSTTYPE' ),
'showposts' => -1 )
); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="">
<?php the_title(); ?>
<?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?>
View
</div>
<?php endwhile; ?>
Thanks!
The problem as wordpress.org documentation says:
Pagination won't work correctly, unless you set the 'paged' query var appropriately: adding the paged parameter
Example of usage:
<?php
$args = array(
'cat' => '5',
'post_type' => 'POSTTYPE',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
?>
<?php the_title(); ?>
<?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?>
View
<?php
endwhile;
?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php
wp_reset_query(); // Restore global post data
?>
Also you can check rvoodoo guide if you have more questions about it.

Categories