I'm trying to query my WordPress for a specific post type "team_members" and then store the titles in an array. When I use the following my output just shows 0, 1, 2 (I only have the 3 posts) it doesn't show the post titles.
$my_array=array();
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post();
$my_array[]=get_the_title( get_the_ID() );
endwhile;
endif;
Any help or direction would be greatly appreciated.
Thanks
John
Thanks Guys,
Your right it does work, I think it was the way I was trying to use the variable in Visual Composer. When I use the variable in a drop down box I see the titles correctly but when I use it for multiple checkboxes it displays the post ID.
It's a Visual Composer issue.
Thanks Mauro as the_title(); did simplify the code and also worked for me.
Thanks
John
I can't see any problem with your code. Try using get posts and using the post_title property instead.
E.g.
$my_array = array();
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$posts = get_posts( $args );
if ( $posts ) {
foreach ( $posts as $post ) {
$my_array[] = $post->post_title;
}
}
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post();
$id = $my_query->posts->ID;
$my_array[]=get_the_title( $id );
endwhile;
endif;
try replacing
get_the_title(get_the_ID());
with
the_title();
Related
I just started working with WP and I'm desperately trying to create my own theme. I currently have been banging my head for a good portion of the day trying to figure out how to limit my query to just two post on my main page. For some reason it will either display all my posts or if I vary the code to have an additional value you in the array 'categories' => 'events' only one post shows up. Thanks in advance for any help you can provide :)
<?php
$args = array('posts_per_page' => 2);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) :
$my_query->the_post();?>
<?php get_template_part( 'events-content', get_post_format() ); ?>
<?php endwhile; ?>
<?php alpha_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'events-content', 'none' ); ?>
<?php endif; ?>
<?php wp_reset_postdata();?>
There are two things wrong with your Query.
The first is that you have posts_for_page in your arguments when it should beposts_per_page.
The second error is you put $args inside an array when you created the new WP_Query(). It should be:$my_query = new WP_Query( $args );
It's been a while since I've worked with PHP and WordPress, so I am a bit rusty. What I want to do is to display the opening paragraph for the most recent post under a category for a WordPress site. Based on some research I did, I've compiled this piece of code:
<?php
$category_id = get_cat_ID('Downtown News');
$post = get_posts( $category_id );
if( !empty( $post ) ) {
setup_postdata( $post );
?><?php the_title(); ?>
<?php the_excerpt();
} ?>
<?php
$post = $wp_query->post;
setup_postdata( $post );
?>
I have code to get the category ID I want, and I have the code to display the first paragraph of the most recent article. However, the code does not seem to work. What is displayed is the first paragraph under an "uncategorized" category of postings, which is not what I want. How can I fix what I have so that it gets the correct category?
You have started at the right place: get_posts, however you don't have the correct parameters. So try the following out:
<?php
$args = array(
'category' => 'CAT_ID_1,CAT_ID_2',
'orderby' => 'post_date',
'order' => 'DESC');
$posts_array = get_posts( $args );
?>
From the function reference we know that:
The category parameter needs to be the ID of the category, and not the
category name
which means that you can have one or more category IDs (comma separated).
A list of all parameters can be found here.
I had the similar issue with yours, maybe this one will work for you
notice the $category_id gets pulled from get_cat_ID() function, this tells me you do not know the cat_id, you can go to the category where you created the downtown news, move your mouse over it to reveal the url address which it will tell you the category id, find that number and replace the line:
query_posts('cat='.$category_id.'&posts_per_page=1');
(using 99 as an example)
query_posts('cat=99&posts_per_page=1');
<?php
global $post;
$category_id = get_cat_ID('Downtown News');
query_posts('cat='.$category_id.'&posts_per_page=1');
if ( have_posts() ) {
?>
<?php the_title(); ?>
<?php the_excerpt();
}
wp_reset_query();
?>
I've inherited a wordpress site and I am having a hard time understanding how posts are being displayed. I want to hide a couple from view (but still be able to give out a URL to view them). I'm not familiar with the way a particular template was coded. The template outputs an image and blurb for each event in a certain category. The meat of code that is spitting this out look like this:
<?php
$args['post_type']='seasonalevents';
$args['posts_per_page']=-1;
$args['orderby'] = 'menu_order';
$activities = new WP_Query( $args );
while ( $activities->have_posts() ) : $activities->the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumb_345_154', true);
?>
Is there any way I can exclude post ID's within the code above? Any hints or tips? Feel totally baffled by this. The variables are defined above this code snippet. I can post if needed.
thanks!
The wordpress-y way to do this would be to add an element to the $args array under the three you already have:
$args['post__not_in'] = array(123,456,789);
Where 123, 456, and 789 are the ids of the posts you want to exlude from showing on this page.
So your whole code would look like:
<?php
$args['post_type']='seasonalevents';
$args['posts_per_page']=-1;
$args['orderby'] = 'menu_order';
$args['post__not_in'] = array(123,456,789);
$activities = new WP_Query( $args );
while ( $activities->have_posts() ) : $activities->the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumb_345_154', true);
?>
Yes there is!
You can get the current post's ID using http://codex.wordpress.org/Function_Reference/get_the_ID
I recommend you looking into 'the loop' and what that is.
This code snippet should do the job :-)
...
$not_these = array(1, 2, 7 /* array with post id's you got somewhere */);
while ( $activities->have_posts() ) : $activities->the_post();
if(in_array(get_the_ID(), $not_these)) continue;
...
The easiest solution is to unpublish that post from your administration panel.
Or
<?php
// The Loop
while($query->have_posts()):
$query->the_post();
if(get_the_ID()!=YOUR_POST_ID):
?>
<!-- Show Post -->
<?php
endif;
endwhile;
?>
I am trying to do something very characteristic and i am not that kinda good with PHP.
I am making some site for soccer team. And i want to have one main page called RESULTS. I will have for example 10 rounds(every round have separate page) in full season. I already make fixtures and everything depending on date.
My problem is how to call specific page, for example today is 29.10.2013 and my 3rd round has just over and i fill out all my result on my "3rd round page", and i want that this content be shown on page "RESULTS" also.
Than next week 04.11.2013 its coming my 4th round and i will also fill out everything on my "4th round page", but i just want now that all this be on "RESULTS" page because this is active round now not 3rd round.
I am guessing that i need to make template for my "RESULTS" page but i just need code, function how to call specific ID page depending on date. (29.10.2013 call page id 54; 04.11.2013 call page id 55).
I hope i explained well and hope someone is good in this.
Thanks in advance!
This is probably not the most elegant answer but should work
<?php
switch(date('d.m.Y'))
{
case '29.10.2013':
$THE_PAGE_ID = 1;
break;
case '29.10.2013':
$THE_PAGE_ID = 2;
break;
case '29.10.2013':
$THE_PAGE_ID = 3;
break;
default:
$THE_PAGE_ID = 4; // page if the date does not match
}
$args = array( 'posts_per_page' => -1,
'post_type' => 'page',
'include'=> $THE_PAGE_ID
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<h1><?php the_title(); ?></h1>
<div id="page-content">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
If you want to get the data of a specific page you can use "get_posts"
http://codex.wordpress.org/Template_Tags/get_posts
Inside your page template add something like this
<ul>
<?php
$args = array( 'posts_per_page' => -1,
'post_type' => 'page',
'include'=> $THE_PAGE_ID
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<li>
<?php the_title(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
Or use "get_post":
http://codex.wordpress.org/Function_Reference/get_post
<?php
$thepost = get_post($THE_PAGE_ID, ARRAY_A);
$title = $thepost['post_title'];
echo $title;
?>
Hope that's what you where after.
Thanks a lot for reply, i see that you very good with this. I did like you tell me and with this i got link to page on my "RESULTS" page.
So is it possible to little bit change code that will be checking date. For example:
check date,
if date is 29.10.2013->call content(not link)of page ID=2
if date is 30.10.2013->call content of page ID=3
if date is 31.10.2013->call content of page ID=4
I would do it myself but i am not even close good with this like you. So sorry for being boring but i really need to learn this stuff.
I'm facing some problem when I fetch page content of particular Id. I'm using the code given below.
There is many pages in my database, I just want to show the page having id =30 and post_type=page. When I'm using the code given below it show all page content having post_type=page. But I just only the one. I think there is some syntax problem in my code.
<?php
$loop = new WP_Query( array( 'post_type' => 'page','ID'=> 30) );
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
?>
Try this
global $wpdb;
$details = $wpdb->get_var("SELECT * FROM $wpdb->posts WHERE ID=30 and post_type='page'");
return $details;
or
<?php
$args=array(
'post_type'=>'page',
'post__in' => array('595', '33', 44)
);
$the_query = new WP_Query($args);
?>
Good luck
Errm, what happened to good old get_post(30)?