This is almost certainly more simple than I assume it to be, but I'm beating my head against the wall here... I've got the following code that simply grabs the most recent 5 posts. What I'd like to do is create an archive that shows the X number of posts that were posted BEFORE the last 5, but I'm having some trouble coming up with the right query_posts variables to do it. Is there any simple way to do this... or should I just skip the first 5 posts that query_posts returns? that seems like the simple way but I thought this might be an opportunity to learn something about query_posts...
query_posts(array(
'posts_per_page' => 5,
'post_status' => 'publish'
));
You're probably looking for the 'offset' function - see this post:
Wordpress get range of posts
Related
Is there a way to get the post time of the latest post under the WordPress archive page?
I would like to add a feature to the archive page to show the latest time of posts published under that category.
Any help, thanks in advance!
I'm sure there are other ways to achieve this, but I can show you one possible way:
You can query your recent wordpress posts using wp_get_recent_posts(), limit it to 1 post.
$recent_post = wp_get_recent_post(
array(
'numberposts' => 1, // get only one post
'post_status' => 'publish' // only published posts
)
);
After that we save the modification date of this post inside of a variable.
$latest_post_date = get_the_modified_date($recent_post[0]['ID']);
You can now output it using echo $latest_post_date; inside of your archive.
Sidenote:
If you want to format the date, the wp_get_recent_posts() function also accepts a parameter for the date format: https://developer.wordpress.org/reference/functions/get_the_modified_date/
This has been driving me nuts trying to figure out: In my plugin I have a section that pulls all entries out of the Database - we'll say all posts, but I also display Pages and Categories - using this line of code:
$args_posts = array(
'numberposts' => -1,
'suppress_filters' => true
);
$posts = get_pages($args_posts);
foreach ( $pages as $page ) {
//formatting & display
}
...and then this get looped, formatted, and echoed using a foreach() statement.
The problem I'm having is that it runs fine on my server but when I have a friend try it it only shows 5 posts. The environment it needs to work under will end up having several thousand entries. Originally, the problem was there for me too but I didn't supply the arguments for get_posts() - I just set it as a variable itself using $posts = $get_pages(), and once I added the arguments it resolved. But my friend is still seeing only 5 entries displayed, even though there's no browser cache or server cache and other changes in the script are coming through.
Thank you in advance!
According to the Wordpress Docs:
Number
(integer) Sets the number of Pages to list. This causes the SQL LIMIT value to be defined. Default to no LIMIT. This parameter was
added with Version 2.8. Note: get_posts() uses the parameter
'numberposts' instead of 'number'. Second Note: it doesn't work if
used together with 'child_of'. Instead use 'parent' and set
'hierarchical' to false.
Link:
Documentation
In otherwords change numberposts to number
I am creating a blog using only Wordpress's backend. I have found functions to get latest posts (wp_get_recent_posts) and all the required data I need. I do this by including wp-load so I have access to WP's functions.
However I cannot find anything that allows me to perform a search outside of Wordpress's theming loops as I have for the rest of the data.
I was hoping there was a search function where I can pass it a search query that could be in title, body content or tag name.
Am I missing something blindingly obvious in the documentation, there seems to be a function for everything else I need outside of WP's "loop".
Does that work for you?
// query for a given term ("search_term")
$search_query = new WP_Query();
$search_posts = $search_query->query('s=search_term');
Source
Answered by sanchothefat:
You can use get_posts() with a search parameter:
$results = get_posts( array( 's' => 'search term' ) );
https://wordpress.stackexchange.com/questions/74763/search-outside-of-the-loop/74766#74766
as always after struggling my mind for 3 days with a problem i decide to ask for help here.
Im trying to create an advance search page with taxonomies and keyword field. You can see what i actually got in http://alianzasuperior.com/empleo/busqueda-avanzada/ ( dont panic, its spanish but just think as taxonomies and you will be ok)
The keyword part is working fine. I can also create taxonomies pages for example taxonomy-job_type.php to handle an individual taxonomy and it works aswell.
The problem is when i try to search in multiple taxonomies , and multiple terms for those taxomies. I tried to figure it out with scribu plugin "query multiple taxonomies" but im not able.
Someone did something similiar for what im trying to do?
Any help or clue will me much appreciate
Like you I am also digging in depth of wordpress multiple search,
check out my question in wordpress stackexchange, it might help you.
Till yet I also had no success in it.
https://wordpress.stackexchange.com/questions/27158/wordpress-multiple-category-search
like other times, after posting here i found my own question. Maybe its not the smarter solution but its working for me.
Im using the new tax_query as commented in http://www.wpmods.com/query-multiple-taxonomies-in-wp-3-1/
Basically if i get 2 taxonomies with two terms for example
$job_type='full-time+free-lancer';
$job_cat='designer+programmer';
I do the following:
$custom_query=false;
$myquery['tax_query'] = array( 'relation' => 'AND');
if ($_GET['job_type']){
$job_type=explode('+',$_GET['job_type']);
foreach ($job_type as $k => $name){
$job_types[]=$name;
}
array_push($myquery['tax_query'],array('taxonomy' => 'job_type','terms' =>$job_types,'field' => 'slug' ,'operator' => 'IN'));
$custom_query=true;
}
if ($_GET['job_cat']){
$job_cat=explode('+',$_GET['job_cat']);
foreach ($job_cat as $k => $name){
$job_cats[]=$name;
}
array_push($myquery['tax_query'],array('taxonomy' => 'job_cat','terms' => $job_cats,'field' => 'slug','operator' => 'IN'));
$custom_query=true;
}
And then if i got more than one term i use the custom query:
if($custom_query) query_posts($myquery);
If you want to mix it with keyword search just do:
$myquery['s']= $yourKeywordVar;
Hope that helps, I just discovered and so far my tests are working fine
EDIT: Seems like my "array-crawling" skills were not enough, thanks for the suggestions.
Moreover, I found out that I was checking the $discounttype condition with a plain "=" instead of a double "==". I guess banging your head on the same block of code for 3 hours makes you dumb and miss the most obvious errors.
First thing first, I'm on Drupal 6.
I have created a form with the following markup:
$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);
The form renders correctly, as you can see from this picture: http://cl.ly/3D2C2h1t1m2B351L1T31
(the N and R values beside the € symbol are actually the value of the $discounttype variable, just for checking it)
Each white box is basically an istance of the beforementioned form.
I need to pass the value of the $discounttype variable on each submit, so I decided to set it as the title of the submit button.
My problem is that in the submit function itself I cannot access the value of the 'title' attribute contained in the #attributes array. Mainly because probably I don't know the right syntax.
So far I've tried
$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];
And every other possible combination, but probably I'm just doing it wrong.
It's actually an hour that I'm crawling the web searching for an asnwer but I came up with anything.
first, you should mention form element ID.
so, you can access submit button by $form_state["cart_".$index]['#attributes']['title'];
but actually, why don't you use hidden field ('#type' => 'hidden') ?
I believe you have to use $form_state instead of $form_values. Give this a try:
$foo = $form_state['clicked_button']['#attributes']['title'];
I recommend using the Devel module while developing for Drupal. It is an extremely helpful tool during development, allowing you to see all the queries run when a page loads, stop a redirect to debug, and much more.