I'm using a custom query to retrieve only posts published in the last 30 days for a custom post type.
I have wp-paginate installed and am using it for pagination. The page itself is working fine, but wp-paginate seems to be showing enough pages for all posts, whether they were returned or not but the custom query.
For example, there are 35 published posts, but only 12 of them in the last 30 days. WP-Paginate should only display 2 pages for all 12 posts in the last 30 days, but it's showing 4 pages, with page 3 and 4 being blank.
My code for the query is:
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
$jobPosts = null;
add_filter('posts_where', 'filter_where');
$jobPosts = new WP_Query('post_type=job_boards&paged=' . $current_page);
remove_filter('posts_where', 'filter_where');
while ($jobPosts -> have_posts()) : $jobPosts -> the_post();
// Display stuff
endwhile; wp_reset_postdata();
if (function_exists('wp_paginate')) wp_paginate();
?>
I seem to have solved it, though it's way more complicated than I think it needs to be. I'm passing the number of pages and the current page to display into the wp_paginate function:
wp_paginate(array('pages' => $num_pages, 'page' => $current_page));
While using custom query try below
For wp pagenavi plugin
if (function_exists('wp_pagenavi')):
wp_pagenavi( array( 'query' => $jobPosts ) );
endif;
For wp paginate plugin
if (function_exists('wp_paginate')):
wp_paginate(false,$jobPosts);
endif;
Related
I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/
I have created a custom post type (activity) with category taxonomy , In the menu the user can select which category he want to display , the problem is that I created category.php but nothing appears on my site here is the code :
<h1><?php single_cat_title(); ?></h1>
<?php if (is_category('edition')) :
$args = array('category_name' => 'edition');
else :
$args = array('category_name' => 'diffusion');
endif; ?>
<?php $query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<p>' . get_the_title() . '</p>';
}
}
?>
I think you can call your file archive-activity.php to make it work .
For your custom post type it's archive-{post-type}.php, and keep the same code you've posted.
The archive-{post-type}.php template provides the most general form of control, providing a layout for custom post type archives, a page that displays a list of posts.
To use different template for your special taxonomy create a file call taxonomy-{taxonomy}-{term}.php or tag-{slug}.php to target specific term.
You can read more about the template hierarchy here : custom-post-type-template-files
I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta
I would like to display search results grouped by post type. I have regular posts, pages,
and a custom post type of product. How would I accomplish this by editing the below code.
The code below just shows all posts and pages right now.
<?php
while (have_posts()) : the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
?>
This code alters the original search query to order the results by post-type in the order you select. There are other solutions, but this is the only one i found that doesn't break pagination or requires multiple queries.
add_filter('posts_orderby', 'my_custom_orderby', 10, 2);
function my_custom_orderby($orderby_statement, $object) {
global $wpdb;
if (!is_search())
return $orderby_statement;
// Disable this filter for future queries (only use this filter for the main query in a search page)
remove_filter(current_filter(), __FUNCTION__);
$orderby_statement = "FIELD(".$wpdb - > prefix.
"posts.post_type, 'post-type-c', 'post-type-example-a', 'custom-post-type-b') ASC";
return $orderby_statement;
}
In your case, I'd do two things:
filter the search page's initial query to a particular post type
use one WP_Query call for each remaining post type
For (1), this would go in your functions.php:
<?php
function SearchFilter($query) {
if ($query->is_search && !is_admin()) {
if (isset($query->query["post_type"])) {
$query->set('post_type', $query->query["post_type"]);
} else {
$query->set('post_type', 'product');
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
For (2), adapt the code you provided from your template file:
<?php
$s = isset($_GET["s"]) ? $_GET["s"] : "";
$posts = new WP_Query("s=$s&post_type=post");
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
wp_reset_postdata();
endif;
?>
You can re-use this code for each other post type.
It's best to avoid using query_posts... see querying posts without query_posts (even WordPress devs agree).
You need to alter the post query to reorder things. You would execute this just before you enter the loop. You can read more about query_posts in the Wordpress codex.
http://codex.wordpress.org/Function_Reference/query_posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('type1', 'type2') ) );
query_posts( $args );
//the loop
I may just be missing this functionality, but does anyone know if there is a widget available:
I need to list the subject for all the entries that are associated with a given tag.
For example: I have 5 articles tagged with "Tutorial", I'd like to see a list as follows:
Tutorial 1: Installing the app
Tutorial 2: Customizing
Tutorial 3: Advanced edits
Tutorial 4: User managment
Does functionality like this exists in wordpress allready?
If you are comfortable with hacking WP you can try adding to your sidebar with wp_list_pages, http://codex.wordpress.org/Template_Tags/wp_list_pages.
Or there are plug-ins like Simple-Tags(http://wordpress.org/extend/plugins/simple-tags/) that help you manage your tags.
The nice thing about WordPress is there are lots of plug-ins available that can add functionality that the base app does not ahve, a quick search for plug-ins for tabs(http://wordpress.org/extend/plugins/search.php?q=tag) returned quite a list, sure it's a lot to dig through but that also helps you see what is available.
So i found an article on using custom queries. I modified the script to pull a specific tag, in this case "Open Source".
<?php
$querystr = "SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->terms wterms, $wpdb->term_relationships wterm_relationships, $wpdb->term_taxonomy wterm_taxonomy
WHERE wterm_relationships.object_id = wposts.ID
AND wterm_relationships.term_taxonomy_id = wterm_taxonomy.term_taxonomy_id
AND wterms.term_id = wterm_taxonomy.term_id
AND wterm_taxonomy.taxonomy = 'post_tag'
AND wterms.name = 'Open Source'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<?php the_title('<li>', '</li>'); ?>
<?php endforeach; ?>
<?php else : ?>
<?php endif; ?>
If you only want to list pages for one specific tag then this would work. However, say you wanted to give a listing of pages for each tag based on the current articles listed on the page.
You might create an array of all the tags using the get_the_tags() function during The Loop and then use that array to dynamically generate the WHERE statement for the query.
You can easily use get_posts to create an array of posts based on a set of parameters. It retrieves a list of recent posts or posts matching these criteria.
In your case, I would like to show how to display your posts under a specific tag ( in your case, Tutorial ) by creating a short code, which can be easily used anywhere later on in your site.
In your functions.php
function shortcode_tag_t() {
$uu_id=get_current_user_id();
$args = array(
'posts_per_page' => 10,
'tag' => 'Tutorial',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
foreach ( $posts_array as $post ) : setup_postdata( $post );
$url = $post->guid;
echo"<li><a href='".$url."'>" .$post->post_title."</a></li>";
endforeach;
wp_reset_postdata();
}
add_shortcode('your_shortcode_name', shortcode_tag_t );
Now you have a list of 10 posts tagged under Tutorial.
Echo the created short code where ever you want to display the list.