Wordpress loop not respecting my arguments - php

I created a WordPress loop for with specific arguments but it is just ignoring them. For example I like to list only posts but there are also pages listed.
<h1 id="" class="offset"><?php _e('Aktuell','Main'); ?></h1>
<?php
// Restore original Post Data
wp_reset_postdata();
// WP_Query arguments
$args = array (
'post_type' => 'post',
'cat' => '50,47',
'numberposts' => '3',
'posts_per_page' => '3',
'ignore_sticky_posts' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article <?php post_class(); ?>>
</article>
<?php }
} else {
// no posts found
}
?>
</div>

This code is working correctly on my site. Perhaps try doing the loop as
<article <?php post_class(); ?>>
<?php echo get_the_title(); ?>
</article>
and confirm if the titles are definitely not what you're expecting

Related

Using a custom Wordpress query to call 5 most recent posts

I am trying to pull the 5 most recent posts of a custom post type using a WP_query. Does the code below look correct? And do I need to use wp_reset_postdata at the end?
<?php
$args = array(
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
);
$most_recent = new WP_Query( $args );
?>
<?php if( $most_recent->have_posts() ) ?>
<?php while( $most_recent->have_posts() ) : $most_recent->the_post() ?>
<div class="webinar">
<h2><?php echo get_the_title(); ?> </h2>
<h3><?php echo get_the_date(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif ?>
You don't need to use wp_reset_postdata() unless you are using WP_Query again in the same page. Usage of wp_reset_postdata() is need to set the post data back
Example
<?php
// The 1st Query
$args = [
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
];
$most_recent = new WP_Query( $args );
if ( $most_recent->have_posts() ) {
// The Loop
while ( $most_recent->have_posts() ) { $most_recent->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
// Updating `$args`
$args['orderby'] = 'post_title'
$args['order'] = 'ASC'
/* The 2nd Query */
$most_recent2 = new WP_Query( $args );
if ( $most_recent2->have_posts() ) {
// The 2nd Loop
while ( $most_recent2->have_posts() ) { $most_recent2->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
?>

how to show last blog post with php?

I have a this code in my WP theme and it works correctly. But when I want to show the last post from another website (for example: www.mag.tabgir.com) it doesn't show the post from my blog.
How can i change this code to show last post from this site www.mag.tabgir.com?
<section class="block-blog box">
<header>
<h2>recent post</h2>
</header>
<section class="content">
<?php
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();?>
<article class="clearfix">
<?php the_post_thumbnail('blog') ?>
<h1 class="title"><?php the_title(); ?></h1>
<span><?php the_time('d/M/Y') ?></span>
</article>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
see more
</section>
</section>
Below is the code it is working correct and tested locally.
$args = array(
'numberposts' => 1,// increase the number if you wish to display 2 latest
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach ($recent_posts as $result)
{
echo 'Title : '.$result['post_title'].'<br/>';
echo 'content : '.$result['post_content'];
}

Wordpress my query post doesn't display my post with category

I am new to wordpress, I have a post that has a category_name of offer and it has a content, here is the permalink : http://localhost/jcjohn/2016/09/20/what-we-offer/
Now I want to display the contents of my post from my section page.
Here is my code inside the section :
<section id = "offer">
<?php
$args = array(
'type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_post()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
So here is what you would need to do to display the post on the single page. You seem to have missed the s from have_post so it needs to be like below
Note: This would go inside index.php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
<?php
if ( $offerBlog->have_posts() ) :
while ( $offerBlog->have_posts() ) : $offerBlog->the_post();
the_content();
endwhile;
else : ?>
<div class="no-content">
<h3>Well... it looks like we forgot to put content here.</h3>
</div>
<?php
endif;
wp_reset_postdata();
?>
</section>
You try using this loop with the cat_id that you have for the category you create.
query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );
You can have a try of the replaced code as follows.
<section id = "offer">
<?php
$args = array(
'post_type' => 'post',
'cat' => '1',
'posts_per_page' => 5,
'orderby' => 'title',
'order' => 'DESC'
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_posts()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
You have missed the loops format.
Reference:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

How to get child posts of current custom post and order it by custom field number?

I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>

Wordpress, if no results from loop, don't show header

I am using WP_Query to loop through a custom post type in wordpress. My loop looks like this:
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
As you can see, before the loop there is a header that says "Available Now!". I want to reformat the loop so if there are no posts returned, then the div containing the title (div class bigRedStrip) will not be displayed. I have tried a number of potential solutions, but the problem I keep running into, is that all of these "solutions" require putting the <div class="bigRedStrip"> inside the loop, which results in the header repeating for every returned post. The idea is to have the header only displayed once. Any ideas how I can accomplish this?
You only need to pull the things a bit apart. First of all run the query:
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
Then check if there is something:
<?php if ($loop->have_posts()) { ?>
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
...
And if so, just iterate over the posts:
...
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php if ($loop->have_posts()){
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>

Categories