wp - all the pages by title - not just the first one - wordpress

How can I get all pages that meet the criteria?
get_page_by_title()
The above only brings the first one. Is it possible for get_page_by_title to return an array of pages?

Here is the code to loop all pages with title:
<?php
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages( $args);
foreach ($pages as $page_data) {
$title = $page_data->post_title;
echo $title;
}
?>
You can change args to filter pages.

Related

remove single_cat_title from wp_list_categories

how to remove or(exclude) current page category(single_cat_title) from wp_list_categories?
I want to remove the current page category from my category list
<?php
$cat = single_cat_title( '', false );
function text_replace( $output ) {
$output = str_replace( '$cat', '', $output );
return $output;
}
add_filter('wp_list_categories', 'text_replace'); ?>
<?php
wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>
You can use the 'exclude' parameter to exclude certain categories. You could get the terms for the post, put them in an array, and pass that to your wp_list_categories query.
<?php
$terms_to_exlude = array();
$terms = get_the_terms( get_the_ID(), 'category' );
if ($terms) {
foreach ($terms as $term) {
$terms_to_exclude[] = $term->term_id;
}
} else {
$terms_to_exclude = '';
}
wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'exclude' => $terms_to_exclude,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>
fix the problem :
<?php
function text_replace($output) {
$current_category = single_cat_title("", false);
$cat = array("$current_category");
$output = str_replace($cat, '', $output);
return $output;
}
add_filter('wp_list_categories', 'text_replace');
?>
<?php wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>

Wordpress - taxonomy dropdown is not working with hierarchical

Any hope to make this Taxonomy dropdown to work with hierarchical.
I added 'hierarchical' => 1 but it seems doesn't work for me!
<?php
if( $terms = get_terms([ 'taxonomy' => 'category', 'hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233 ]) ) :
echo '<select name="categoryfilter4"><option>Downloads...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
use wp_dropdown_categories function
https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
<?php wp_dropdown_categories( ['name'=>'categoryfilter4', 'show_option_none' => 'Downloads...','hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233, 'order_by' => 'parent'] ); ?>
<?php wp_dropdown_categories( $args ); ?>
<?php $args = array(
'show_option_all' => '',
'show_option_none' => '',
'option_none_value' => '-1',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'include' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'value_field' => 'term_id',
); ?>

WooCommerce category and sub category loops

When building a WooCommerce site they have made it really easy to display categories and sub categories on the archive and category pages.
However does anyone know if it's possible to add a the list of categories/sub categories to the (content-single-product) page template?
I have a store that had only a handfull of products and we would like users to be able to quick select from the side menu rather than go back and forward between the archive page and the product pages.
Thanks to anyone who can help.
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$return .= '' . $woo_cat_name . '';
}
$prod_cat_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
you can also edit all fields try this

Pagnation wordpress custom post type category page

Can't get the pagnation to work on the custom post types category page. It works when displaying the custom archive page. When I click on the pagnation it shows the posts from the first page but the URL says page=2.
This is the code i'm using in the archive-slug.php. How can I customize it to work with the taxonomy-slug.php?
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'post_type' => 'dropshippers', 'paged' => $paged ) );
$loop = new WP_Query( array( 'post_type' => 'dropshippers', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 8 ) );
if(have_posts()) : while(have_posts()) : the_post();
//Posts
endwhile; endif;
if(function_exists('wp_pagenavi')) {
wp_pagenavi( array( 'query' => $loop ) );
} else {
echo "No posts";
}
You need to add the name of you category in your query, i prefer to use get_posts for that:
<?php $args = array(
'posts_per_page' => 8,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'dropshippers',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Fullfit this two line with your args:
'category' => '',
'category_name' => '',

WordPress: First Comment ID from current post

Does someone know how to get the first comment ID from the current post?
I have already searched long for a good solution, but found nothing suitable.
I appreciate any help.
get_comments() returns an object. So you can just access the first one like any other object:
$comments = get_comments();
$first_comment_id = $comments[0]->comment_ID;
You have to use the following function to get comments of a post
<?php get_comments( $defaults ); ?>
It's default usage is as follows:
<?php
$defaults = array(
'author_email' => '',
'author__in' => '',
'author__not_in' => '',
'include_unapproved' => '',
'fields' => '',
'ID' => '',
'comment__in' => '',
'comment__not_in' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_author__in' => '',
'post_author__not_in' => '',
'post_ID' => '', // ignored (use post_id instead)
'post_id' => 0,
'post__in' => '',
'post__not_in' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => 'all',
'type' => '',
'type__in' => '',
'type__not_in' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '',
'date_query' => null, // See WP_Date_Query
);
?>
Make sure to edit the values as per your requirement. Using get_comments() you can get all comments and this way you can fetch first or any required comment id using loop.

Resources