Fix wordpress query (add if have) - wordpress

<ul>
<?php
global $post;
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
?>
</ul>
How can I put the into if have posts query? It's basically custom post type query but its using document revisions plugin (so get_posts is get_documents)?
Many thanks!

If you just want to check if the query returned any posts. You can still do it with your code.
Here is example:
<?php
global $post;
// This is your query
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
if (count($myposts) > 0 ) : // If the query returned results
?>
<ul>
<?php foreach( $myposts as $post ) : setup_postdata($post); // for each post in array ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Related

Advanced custom field query in WordPress

I am trying to list all the post while advanced custom field value ispremium=>yes and post issticky in my WordPress site. I have following code in my page. It is listing all the post but not checking ispremium=>yes and issticky post rather showing all the posts.
What's wrong in my code?
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args and is_sticky());
?>
<?php if($the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Try this:
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' ),
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes',
'ignore_sticky_posts' => 1,
);
$the_query = new WP_Query( $args );

How to display posts with Category named news

I just created a new page template and want to print all posts with category news only.
Used this code based on wordpress codex when it works.
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'posts_per_page' => 3 )
);
// The Loop
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile;
// Reset Query
wp_reset_query();
?>
How can wrap the title into an h1 tag and the content into a div box?
I tried this but it is giving me syntax error:
<h1><?php the_title(); ?></h1>
Hope you can help.
<?php global $post;
$args = array(
'posts_per_page' => 3,
'post_type' => 'post',
'category' => 1
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title( '<h1 class="entry-title">', '</h1>' );
endforeach;
wp_reset_postdata();
?>
I think you should remove category = 1 in a parameter of the arguments.
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'offset'=> 1,
'category_name' => 'news'
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title();
the_content();
endforeach;
wp_reset_postdata();
?>
Note: Please use slug of category in category_name. Also you have syntax error in
<?php the_content(); ?>"> //removed ">
Hope this will work for you. Thank you
I have added div tag for content & h1 tag for title & updated your code.
Please find your updated code:
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 3
)
);
// The Loop
while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<div class="wrap">
<?php the_content(); ?>
</div>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
Hope, this will help you.

How can I get the latest post from a category in WordPress?

I am designing the category page. I have a loop that displays the child categories for the current category. For each child category, I would like to display a link to the latest article. Currently, the link is the same for all child categories, even when the displayed article is not in that category. What am I doing wrong?
<?php
$cat_id = get_query_var('cat');
$categories = get_categories(array( 'parent' => $cat_id));
if(count($categories) > 0):
foreach($categories as $cat):
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$the_query = new WP_Query( $args );
$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata(); ?>
<div class="media category-list">
<div class="media-body">
<div class="details">
<h3><?php echo $cat->name; ?></h3>
<p><?php echo $cat->description; ?></p>
</div>
<dl>
<dt>Article Total:</dt><dd><?php echo $cat->count; ?></dd>
<dt>Last Article:</dt><dd><?php echo substr($recent["title"], 0, 48).'...'; ?></dd>
</dl>
</div>
</div>
<?php endforeach;
endif; ?>
Looks like you are using get_posts arguments in WP_Query.
category and numberposts are not valid arguments for WP_Query
Both belongs to get_posts and internally converted to cat and posts_per_page
So when you pass these arguments in WP_Query it does not work. But when you pass WP_Query arguments in get_posts it works ;)
So the updated arguments structure is
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'cat' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
Also please update your query with this
$the_query = new WP_Query( $args );
//$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
// wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata();
the_post should be called only once and after when you are sure that query has posts.
wp_reset_postdata hold the data for whole query. So need to do this at end of while loop not inside of the while loop.

permalink for multiple category

hi i had 15 pages for 2 categories.. example category A and category B.... while doing so i have displayed title and content... but permalink is creating for category A not for category B... how can i code for to get a permalink for second category...
$cat = get_the_category();
$id= $cat[0]->term_id;
$id1= $cat[1]->term_id;
$args = array('posts_per_page' => 15,
'cat'=> $id1,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged,
'post_type'=> 'post',
'post_status' => 'publish' );
query_posts( $args );
if ( have_posts() ) while ( have_posts() ) : the_post();
$i++;
?>
<li><?php the_title();?></li>
<?php
endwhile;
wp_reset_query();
?>
but the permalink is creating for $id...
if you are using template or page to display this two categories post. You have to run two loop for every category. If you are trying to use shortcode or something. it will be different.
$cat = get_the_category();
$cat_01= $cat[0]->term_id;
$cat_02= $cat[1]->term_id;
$args_for_cat_01 = array('posts_per_page' => 15,
'cat'=> $cat_01,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged, //I don't know what is this $paged??
'post_type'=> 'post',
'post_status' => 'publish');
$post_from_cat_01 = get_posts( $args_for_cat_01 );
foreach ( $post_from_cat_01 as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
//Do whatever you want here. Use divs or anything.
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
As above like this. Create another args for your second loop.
$args_for_cat_02 = array('posts_per_page' => 15,... change the cat => cat_02 ..ect
Then use the loop as above.
$post_from_cat_02 = get_posts( $args_for_cat_02 );
foreach ( $post_from_cat_02 as $post ) : setup_postdata( $post ); ?>
Remember to change the variables. I just use for more explanation.
If you need my help. Please find me any of social network by searching yeshansachithak.

Pagination for my custom post type in widget

I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
Try this one and enter your custom post type's name in 'post_type' => 'your custom post type name'
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'paged' => $paged1,
'post_type' => 'your custom post type name'
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
?>
<?php endif;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $previous_post->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
wp_reset_postdata(); ?>
</ul>

Resources