My client has a custom post type called 'Clients'. She uses this to record all information about each client she has. She then sometimes leaves these in either draft mode or published. She would like to be able to see all of her clients at a glance and copy and paste all of their details straight from the web page on a regular basis. I have created the following query (note the query is only showing fist and last name at the moment for testing):
<?php
$args = array(
'post_type' => 'clients',
'post_status' => array('draft', 'publish'),
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$fname = get_field('fname');
$sname = get_field('sname');
echo '<p>'.$fname.', '.$sname.'</p>';
endwhile;
?>
However, this returns no results. When I change the 'posts_per_page' to a number, the query works. However, if this number is above 58 no results are returned again. I think somewhere there is a limit to how much can be output in 1 query. I cannot use pagination at the minute as the client is admit she'd want to see all information on one page. Is there another way of querying this without limits? Or a way to adjust the limit?
Thank you in advance
Without being able to debug it directly I'm not sure why you're seeing this issue. Alternatively you can try using the 'nopaging' => true option instead of the 'posts_per_page' option. Does that method behave?
Are you sure that the custom post type name is clients and not client? That is the only thing that I can point, as I the only parameter that is custom by the developer ^^.
The problem was down to my local setup, as soon as it was uploaded to the clients server the error disappeared. Sorry I forgot to close the question
Related
<?php wp_dropdown_categories(); ?>
→ This is how we can populate the categories in a drop-down.
N.B. We are dealing with WordPress.
This is how we generate a query in Wordpress:
<?php
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'category_name' => 'staff'
) );
?>
I need some help in php here →
'category_name' => 'staff'
staff is a category, for example, here. How can we write in terms of PHP or Ajax so that whatever category is chosen from the populated category from the drop-down should come above dynamically based on the selection from the drop-down?
Means → 'staff' This should come up dynamically from the drop down. I am normal in PHP and ajax If someone can guide me than help will be highly appreciated.
As we can see in the documentation of wp_dropdown_categories the input name is cat (or you can change it to anything you like and does not affect other parts of your code).
So in your PHP file do sth liek the following:
<?php
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'category_name' => $_POST['cat'] // or $_GET['cat'] or $_REQUEST['cat']
) );
?>
Make sure to validate and sanitize your POST/GET input to avoid problems
[UPDATE] If you use ajax then you have one php file that handles the ajax request using an action (for example take a look here). Then in your web facing php page use jquery or similar to post (or get) the ajax request and return the categories or posts using WP_Query like shown above. Read the article mentioned to have an idea how to implement ajax in Wordpress.
If you have further questions with the implementation please provide specific information so it can be helpful.
I'm trying to add a meta box for my custom post type (which is inside a plugin).
I want to get all pages that are assigned a particular page template in my Wordpress site, so I use the following:
$surgeons = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-meet-the-team-single.php'
));
However, $surgeons is returned as an empty array, why?
It sounds like no page is using the template I'm stating, as if I just use the default get_pages() it returns all pages, but this is incorrect as I definitely have pages that are assigned this template.
Does it matter that this code is in a plugin file, and the meta_value is trying to locate this template in the plugin folder?
Although I don't think this really provides an answer to the issue I Was having, this was the way to fix the array being returned and provide the correct results.
Instead of using get_pages(), I simply switched it to get_posts(), which then returned the correct results.
$surgeons = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_key' => '_wp_page_template',
'meta_value' => 'template-meet-the-team-single.php'
));
Please try by using custom query:
$all_team_single = query_posts(array(
'post_type' =>'page',
'meta_key' =>'_wp_page_template',
'meta_value'=>'template-meet-the-team-single.php',
));
Your above code looking fine , to get more help please visit the below link. https://wordpress.stackexchange.com/questions/173469/loop-through-pages-with-specific-template
I'm trying to create a "load more posts" button for my Wordpress blog page using AJAX. The Javascript function that takes care of passing the 'paged', 'offset' and 'posts_per_page' parameters is doing so successfully (I've checked a hundred times!).
The offset and posts_per_page parameters work fine (I repeatedly changed their values to test this) but the WP_Query always returns the posts on the front page, even though the value of $page changes each time more posts are loaded.
Just incase the $paged value was getting messed up somewhere, I used static numbers for the 'paged' parameter but it still only shows first page posts no matter what the value.
Everything executes fine and I receive no error message, with debugging enabled. The posts do load - just not the right ones.
Other info: my own theme, no plugins running right now.
The code below is from a function in my functions.php file.
$args = array(
'suppress_filters' => true,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => $ppp,
'paged' => $page,
'offset' => $offset, //I've tried simply putting 3, 5 etc
);
$loop = new WP_Query($args);
if ($loop -> have_posts()) :
while ($loop -> have_posts()) : $loop -> the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
I've only entered what I believe to be relevant code, in order to make it easier to read. Let me know if you'd like me to add something. And thank you!
EDIT: Using the query() function directly makes it work but I really don't want to mess with the main query object...and this just means that everything else in my code is fine. WP_Query is just being weird.
You can't used offset and paged together - you would only use one or the other. Using them together breaks pagination. If you want the posts that would appear on page 2 (assuming you have 10 to a page), use either:
'paged' => 2
or
'offset' => 10
This is my first post in this great community. Normally I find all the answers to my small and large problems, but this I'am sad to say is a mystery to me :(
I need to create some kind of automated function that every day, once a day chooses 3 random products and marks them as featured in my Woocommerce store. And the next day removes these 3 and chooses 3 new once.
Sadly due to complicity of the task(for my), I have no code to show/start from.
Only thing I can find is that it has to be defined in some meta information:
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
)
Truely hope someone can help me :)
I think the easiest solution would be to use transients.
Here's a suggestion, but first a couple of caveats.
if no one visits your site the transients don't always clear at the right times, but if that's an issue you can set up a ping service to automatically hit your site every day.
you don't have precise control over when the transient clears... ex: every day at 3pm.
This suggestion requires you to rework how your theme is displaying these 3 featured products. since we aren't actually looking for the _featured meta.
code for functions.php:
function so_35312355_get_daily_featured_products(){
// Get any existing copy of our transient data
if ( false === ( $daily_featured_products = get_transient( 'daily_featured_products' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'rand',
'posts_per_page' => '3',
);
$daily_featured_products = new WP_Query( $args );
set_transient( 'daily_featured_products', $daily_featured_products, 24 * HOUR_IN_SECONDS );
}
return $daily_featured_products;
}
Alternatively, you could run 2 queries.... one to reset all the featured meta keys and a second to update the meta keys for each of your random 3 products.
A more advanced and complete method would be to use cron jobs. Take a look in the Codex for how to schedule an event, but depending on your needs the transient may be enough.
I have an android application that need to download (via webservice - php ) some books from a wordpress website.
Those books are structured like this : Book Name -> Category 1 -> Subcategory 1 -> text
All information is stored in wp_posts and all realtions are made with wp_postmeta.
I try to begin from "top-to-bottom" starting with "Book Name" like this :
$args = array(
'posts_per_page' => 8,
'orderby' => 'rand',
'post_type' => 'biblie',
'post_status' => 'publish'
);
$show_albums = get_posts( $args ));
and then with wp_query() using post_type, meta_key,meta_value
But any step i take with this method it gets more and more puzzling
So .... is there a way to have all the information using one interogation? I want to interogate wp by book name and somehow have all the information resulting in one arrayor it is to much from wordpress ?
Hope you understand my english !
Thanks
There are plugins that give you a restful api out of the box which could do what you want. A quick search gave me these:
https://wordpress.org/plugins/json-api/
https://github.com/WP-API/WP-API