Wordpress show number of posts within the serie and loop? - php

I am not sure how to describe my request, which means that I have had a hard time Googling it. Anyway what I am looking for is the following:
Lets say I have a archive with 15 posts. The archive page is restricted to 10 posts per page. Now I would like to show on the first page post 1-10 and on the second page post 11-15. I tried to google it but with no luck. All I did find was a general wp_query post count:
<?php
echo $wp_query->post_count;
?>
I guess it shouldn't be too hard to accomplish correct? Anyone who can help me out here?
Thanks

the thing that you wanted is so easy,
wordpress query do this default for you,
install wp-pagenavi plugin its show a pagination non the footer and only thing you need to do is set the post limits on 10 in wordpress general setting section.
if this is not what you wanted, so please explain with an example.
********** i found out what you mean of this ***********
first you have to get posts_per_page , you can get that value whit usin one of these codes
$default_posts_per_page = get_option( 'posts_per_page' );
or
global $wp_query;
$total_posts = $wp_query->post_count;
after that you need to know witch page your in , so you can use this code to get that
$current = intval(get_query_var('paged'));
now we need a simple calculate to make the numbers you needed,
<?php
$ppp = intval(get_option( 'posts_per_page' ));
$current = intval(get_query_var('paged'));
$last = $ppp * $current;
echo 'post from : ' . ( ($last+1) - $ppp). ' to : '.$last;
?>

Related

How do I generate numeric titled wordpress posts?

I would like to automatically generate some sequential Wordpress posts that have numeric titles, for example "1", "2", "3"... etc.
I'm looking for an automatic system since I would like my posts to go from "1" to "999999".
No post content or meta is required, only the titles.
I'm aware of wp post generate but I have no idea on how to use it so that the post titles have the requirements explained above.
I'm working on a demo project so search engine optimization is not a concern at the moment.
CLI usage is accepted.
You can achieve that quite simply by using a for loop with any arbitrary range (1-99 for example - or any other range you prefer)
for i in {1..99}; do wp post create --post_title="This is post number $i" --post_status=publish; done
This will call the wp post create command on each iteration of the loop and append the number to the post title.
<?php
// Create post object usinf for loop
for($i=1;$i<=9999;$i++){
$my_post = array();
$my_post['post_title'] = '<?php echo $i;?>';
$my_post['post_content'] = 'This is my post-<?php echo $i;?>';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );"
}
?>
wordpress appends a numeric value automatically if there's an existing post with the same post-title.
so if the permalinks are set to be postname the URL will look as follow:
my-post
my-post-1
my-post-2
etc

Trying to add two return values into one shortcode PHP WordPress (total number of posts and pages)

I am new to php, please help.
I am using this shortcode to show total number of published posts (400), it works fine. If I change ('post') to ('page') it shows total number of published pages (100) just as fine. I am trying to show total number of posts and pages (500) using one shortcode. Is it possible to add the two results and display as one total result?
function published_posts($atts) {
return wp_count_posts('post')->publish;
}
add_shortcode('posts_count', 'published_posts');
Yes, what you're attempting to do is possible. Simply assign the value of wp_count_posts() for both posts and pages to variables and then add them together in the return statement.
Example:
function published_posts( $atts ) {
$post_count = wp_count_posts( 'post' )->publish;
$page_count = wp_count_posts( 'page' )->publish;
return $post_count + $page_count;
}
add_shortcode( 'posts_count', 'published_posts' );
While this will work, I'm not convinced it's the most efficient approach you could take. It's making two trips to the database for information which could be retrieved in one. That being said, wp_count_posts() won't accept an array of post types so you'd likely have to code something from scratch.

Wordpress Plugin The Events Calendar - Listing All Events For One Year

I've been struggling (I'm a newb) on how to have The Events Calendar print a list of all events within a specified year. I researched this need heavily online and I'm surprised that there's limited info on the subject. The list is simple really. It just needs to be a linked title that sends the user to the actual event page.
My approach was to create a custom [shortcode] in functions.php so I could place the list on any WordPress page or post that I need.
The custom shortcode PHP is the following.
add_shortcode('yearList', 'yearList');
function sayHello()
{
echo '<h1>Hello World</h1>';
}
I added this to the functions.php file within the Genesis child theme root. The text echoed fine so that part is good to go.
After researching the forums at Modern Tribe I came across a useful page that reviews the tribe_get_events function. Here's the page for those wondering.
Any way, the code in this page gets me close so I know I'm on the right track.
The PHP that I'm using within the created shortcode function is the following.
// Setting up custom function called yearList
add_shortcode('yearList', 'yearList');
function yearList()
{
// Ensure the global $post variable is in scope
global $post;
// Retrieve all events desired year. Only eight printed out of 80.
$events = tribe_get_events( array(
'eventDisplay' => 'custom',
'start_date' => '2014-01-01 00:01',
'end_date' => '2014-12-31 23:59'
) );
// Loop through the events: set up each one as
// the current post then use template tags to
// display the title and content
foreach ( $events as $post ) {
setup_postdata( $post );
// prints the title for each event.
echo '<br><br>';
// WRONG WRONG - This is below is what's throwing me.
echo '';
//the_title();
// echo tribe_get_start_date(); This shows the start date and time after the event title I slept this for now.
}
}
I have two questions.
Why would the loop only show nine titles when I have over 80 stamped
for 2014?
How can make each line be a link to The Calendar Events actual event
page? I've tried to employ this with no luck.
Thanks for any help on this.
For your first problem, try adding posts_per_page => -1 inside your $events = tribe_get_events( array( ... ); statement. The default limit must be set to 9 posts somewhere. If that doesn't work, try replacing -1 with a large number.
For the output issue, you can't echo an echo in php. Try this instead:
$ev_link = tribe_get_event_link();
$ev_title = get_the_title();
printf('%2$s', $ev_link, $ev_title);

Wordpress Future Page Issue (Not Showing)

I've been asked to fix a problem on a wordpress site. The problem is caused by the fact posts should of been used instead of pages. However the site is up and running and quickest option would be to fix the problem using a hack of sorts.
The site has an events section, each event is a page (this is the issue, should really be a post). In order to have upcoming and past events the post date is used, therefore upcoming events has a post status of 'future'.
There's are list page of events which shows them fine by using the correct query_post(). Although the problem arises when you click through to actual event (which is a future page). If your logged in as an Admin then the page shows but if your not logged in you get a 404 page.
Now if they where posts then the "The Future is Now!" plugin would solve this problem. I have a feeling the only way round the problem is to rewrite part of core Wordpress files.
Any advice would be great, I have a lot of experience with Wordpress so even if you can point me in the right direction.
Cheers,
Jason
[Update]
Thanks maiorano84 for your detailed response. In the long run I intend to move them to posts but in the meantime they've requested we fix it asap without changing any urls (today they sent out a mass email with a list of events without checking any links)
Your solution of including post_status future doesn't work in this case as wordpress doesn't get to the stage of loading the template. Something in wordpress core stops it from getting that far. Ideally if they was a hook I could use to override this behavior in the meantime that would be excellent but if it comes to it I will temporarily edit the core files.
[Update 2]
I now know the two functions that need editing or use a hook that relates to them.
First of we is_404() which needs changed to not add future pages as 404
Second we have is_page() need to return true if a page is future
[Update 3]
I've found how to do this in the core. If you go to wp-includes/query.php line 2682. Copy this in instead of the old function it works correct. If you have a better way please let me know. Thanks.
/** Future Pages **/
// Check post status to determine if post should be displayed.
if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
$status = get_post_status($this->posts[0]);
$post_status_obj = get_post_status_object($status);;
if ( !$post_status_obj->public ) {
if ( $post_status_obj->protected ) {
$this->is_preview = true;
print_r($status);
if ( 'draft' != $status ) {
$this->posts[0]->post_date = current_time('mysql');
} else {
$this->posts = array();
}
} elseif ( $post_status_obj->private ) {
if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
$this->posts = array();
} else {
$this->posts = array();
}
}
/** END **/
You can actually add a post_status parameter of 'future' to your page queries. You REALLY shouldn't be modifying your core files in order to do what you want. So on your page.php and archive.php templates (or other relevant templates that's controlling your Event display), you can do something like this:
<?php
$params = array('posts_per_page'=>-1, 'post_status'=>'publish,future');
$query = new WP_Query($params);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<!-- YOUR PAGE HTML HERE -->
<?php
endwhile;endif;
wp_reset_postdata();
?>
This is an oversimplification, but using the correct queries in the corresponding files will allow you to display your pages however you like.
More information here: http://codex.wordpress.org/Class_Reference/WP_Query
Another thing worth considering, and I realize that this wasn't a part of your question, but may very well solve your issue:
Why not create a subdomain on your client's server where you can work on fixing everything without interrupting the user experience? You can easily either import the existing database into your development environment, and make all the changes you need without affecting the live version.
Food for thought. My advice would be to nip this in the bud, and convert the pages over to posts as soon as possible, otherwise the website will turn into a giant mess very quickly, but that's your call.
Hope this helps.
UPDATE:
I would still advise against altering the core files, but if it's a last resort until everything gets fixed, then have at it. Here's is a "WP Friendly" solution I think might help:
add_action('wp','future_redirect', 0);
function future_redirect($WP_Object)
{
$page = is_page() ? get_page(get_the_ID()) : false;
if($page && $page->post_status == 'future')
{
wp_redirect(/*YOUR REDIRECT URL*/);
exit();
}
return $WP_Object;
}

Wordpress Ordering Issues

I've got a theme that I'm using and it's throwing posts up on a slider on my home page but I would like to order the way they show up. The code currently in place is:
<?php
//OptionTree Stuff
if ( function_exists( 'get_option_tree') ) {
$theme_options = get_option('option_tree');
$homeCategory = get_option_tree('home_category',$theme_options);
$homeNumber = get_option_tree('home_number',$theme_options);
}
?>
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('cat='. $homeCategory .'&showposts='. $homeNumber .''.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
And the way I would like this to be ordered would be the following:
orderby=meta_value&meta_key=event_date&order=ASC
I've never used option tree before in obtaining information from some theme options, so I'm kind of confused on how I would integrate that ordering method with the other code. Any help is much appreciated, thanks!
Jarth
I would need to dig through the documentation for the option tree, but I definitely recommend retrieving the values into variables before you plug them into the query. For example, fist dump the meta value into a variable. Then retrieve the values you want from that array... and so on. At any point you can do a var_dump() to make sure you're getting the expected values.
Once you have that working, you should be able "orderby" the appropriate column.

Categories