Showpost and last wordpress - wordpress

I need your help.
I would like to bring up the last (number) from my template classified ads. Here's the code:
<?php
$the_query = new WP_Query( array(
'showposts' => 10,
'post_type' => 'ad',
'tax_query' => array(
array(
'taxonomy' => 'ad_category',
'field' => 'id',
'terms' => array('8')
)
)
));
if($the_query->have_posts()):
?>
<div class="feat-ad column col12"><!-- featured ad -->
<h3 class="widget-title">Category</h3>
<ul id="first-carousel" class="first-and-second-carousel">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php colabs_image('width=150&height=150&play=true');?>
<?php the_title();?>
</li>
<?php endwhile; ?>
</ul>
</div><!-- end featured ad -->
<?php endif;?>
I tried with showpost, but nothing appears in one ad ... this is not even the last announcement.

Use the posts_per_page parameter instead of showposts
posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1
http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

Related

Show tags that are the same as the title of the custom post wordpress

I'm at a loss. On my single{custom-post-type}.php I want to create a loop that first shows the title of the page and the content. But then it also generates a list with all the content from a tag that had the same name as the title of the page.
Can somebody help me on the way?
You need an instance of WP_Query to query the posts inside the taxonomy term with the same name as the title. This is done by adding the tax_query field to the arguements.
<?php
// WordPress header
get_header();
the_post();
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1, // -1 retrieves all the posts the query finds.
'tax_query' => array(
array(
'taxonomy' => 'category', // This is the default 'post' taxonomy, if you are using a custom taxonomy then you need to change this.
'field' => 'name', // Use name as you want to match the title
'terms' => get_the_title(), // Get the title of the current post as a string.
)
),
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()): $query->the_post(); // Loop through the posts from the term with same name as current post title. ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile;
wp_reset_postdata(); // Reset usage of 'the_post()'
endif;
<?php get_footer();

How to display the latest news and blog posts in a front page using wordpress?

I'm using wordpress to create my own website. On the front page I want to dispaly the two latest blog posts and the two latest news article.
An example of text to display is like that:
<div class="blog_bloc">
<span class="Title_post">Title</span>
<p class="text_post">blog post text text text text text</p>
<button id="read" >
Read more
</button><br><br>
<span class="Title_post">Title</span>
<p class="text_post">blog post text text text text text</p>
<button id="read" >
Read more
</button>
</div>
I found some plugins in wordpress for creating blog page and news page.Now I want to display the latest posts or articles from the blog page or news page to put it automatically in the front page.Is there any plugin to add in my template to scrap the latest articles and put them into the front page automatically?
To get the latest posts use this php function:
<?php
$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
This will return an array which you then can loop through to display the two latest posts.
An example script would look like this:
<?php
$args = array(
'numberposts' => 2,
);
$recent_posts = wp_get_recent_posts( $args );
foreach ($recent_posts as $post ) {
?>
<div class="blog_bloc">
<span class="Title_post"><?php echo $post["post_title"]; ?></span>
<button id="read" >
Read more
</button>
</div>
<?php
}
wp_reset_query();
?>
It will normally show five posts in homepage or anywhere.
// Define our WP Query Parameters
<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
// Start our WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Display the Post Title with Hyperlink
<?php the_title(); ?>
// Display the Post Excerpt
<?php the_excerpt(__('(more…)')); ?>
// Repeat the process and reset once it hits the limit
<?php
endwhile;
wp_reset_postdata();
?>
Use your own css.

Pagination shows empty pages for date based archive.php loop

I'm using wordpress and have a custom archive page for a custom post type.
The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
Working with Custom Archive and Pagination
#Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.

WP Custom taxonomy pagination

I have a custom taxonomy (support) and a custom post type (question) both related.
In my taxonomy-support.php template file, I use the following query:
<?php
$current_category = get_term_by('id', get_queried_object()->term_id, 'support');
$questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
And the loop
<?php if ($questions->have_posts()): ?>
<ul>
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
<div class="clearfix">
<div class="pull-left">
<?php previous_posts_link('← ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
</div>
<div class="pull-right">
<?php next_posts_link(__('Next', 'my-theme') . ' →', $questions->max_num_pages); ?>
</div>
</div>
<?php endif; ?>
As you can see I defined 2 posts per page.
When I visit the page, it shows me 2 posts then I go to page 2, it still works but when I go to page 3 or up, it shows 404.
Any idea?
I'm using WordPress 3.8.2 with no plugin installed.
Thanks
I was happy to find your post as I was fighting for the exact same issue (less happy to find no answer yet ;) ).
Anyway I finally found something interesting here: https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy
This made the trick for me.
I reckon you were into the situation where the value for your global posts_per_page option (as set up in the admin) was greater than the one for your taxonomy, which made the system screw up.
Well, not that much when you think about it: during its lifecycle, having the global posts_per_page option as is, the request does not validate the "paginated" url, thus never even load your taxonomy-support.php template file, where you tell it that the posts_per_page should be different.
You then must tell it earlier, during the intialization process. The 'pre_get_posts' action is the one.
Hope it helps someone!

Wordpress - Sorting post loop by meta data date

I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.

Resources